4

If I have a bash script stored in a file I can execute it as follows:

./path-to-script.sh param1 param2 param3

now if I have that same script stored in a variable:

script="$(cat ./path-to-script.sh)"

How can I execute it with those same parameters?

(For context - I'm running the script on a remote computer, and have to copy paste it into the command to send. I could echo it into a file in the remote computer and execute that, but if I can avoid touching the file system that would be better).

0

1 Answer 1

3

Like this:

bash -c "$script" my_script param1 param2 param3

my_script is gonna be the name of your script and appear in error messages. For example:

$ bash -c 'echo ${1bad_var}' my_script
my_script: line 1: ${1bad_var}: bad substitution
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.