Skip to content
Advertisement

bash command not found when setting a variable

I am writing a shell script where I am setting few variables, whose value is the output of commands.

The errors I get are:

JavaScript

now, I did read other similar questions and based on it, I tried various things:

spliting command into two calls

JavaScript

tried using backticks

JavaScript

tried using $()

JavaScript

But in every case the command output is getting executed. how do I make it to stop executing command output and just store as a variable? I need this to work on ZSH and BASH.

Advertisement

Answer

You define variables with var=string or var=$(command).

So you have to remove the leading $ and any other signs around =:

JavaScript

From Command substitution:

The second form `COMMAND` is more or less obsolete for Bash, since it has some trouble with nesting (“inner” backticks need to be escaped) and escaping characters. Use $(COMMAND), it’s also POSIX!

Also, $() allows you to nest, which may be handy.

Advertisement