nullByteMe
nullByteMe

Reputation: 6391

cat a file to a variable

Assuming the name of my script is myscript.sh and my current directory is /Users/scripts/ I'm trying to do the following:

localScript=$(cat ./myscript.sh)

I get the following error:

#!/bin/sh not found

I can't seem to figure out how to do this, but I assume its not working because $() is creating a subshell that has a different pwd and thus cannot find my file.

I've also tried using various combinations of pwd but I'm having trouble with this method as well.

Upvotes: 1

Views: 244

Answers (1)

jbr
jbr

Reputation: 6258

On OSX I've done the following:

$ vim test.sh

and typed in the following:

#!/bin/sh
localScript=$(cat ./test.sh)
echo $localScript

and then,

 $ chmod +x test.sh
 $ ./test.sh

which gives the following output:

#!/bin/sh localScript=$(cat ./test.sh) echo $localScript

Maybe the above will help you spot your error.

Upvotes: 2

Related Questions