I came upon this strange behaviour in my bash script.

alias $V="echo test"
echo $(a)                 #returns 'test'
echo $($V)                #returns ...'a: not found'

Is there any way to emulate the former behaviour with the variable?

  • What OS and bash version are you doing this with? I get errors on both echo lines on Debian Jessie with bash Jul 24 '16 at 21:27
  • @StephenHarris yes you get error on both lines. What the author wants is why a variable cannot be like an alias and execute the command.
    – user172564
    Jul 24 '16 at 21:40
  • @FarazX The comments in the original say he did not get an error in both lines; in his case the alias was defined; in my case it wasn't. Now with ksh93 I get the results he gets, so the evaluation is definitely shell and version dependent. Jul 24 '16 at 21:42

Aliases are only expanded if the command appears directly in the code, without any expansion. Writing things like \a, $V, $(echo a), etc. suppresses alias lookup.

In addition, bash (unlike other shells) doesn't expand aliases in scripts by default anyway, so a actually does not run the alias in bash.

Use a function instead of an alias. You'll need to use the original name to define the function.

a () { echo test; }
"$V"     # prints test

(There are other ways to do something like what you want by using eval, but don't use eval unless you know exactly what you're doing. Quoting things correctly with eval is tricky.)


It like you're saying:

alias $V="echo test"
echo `a`              #echo the out put of the 'echo test' command which is test 
echo `$V`             #echo the output of $V 'command' which is holding a value itself and it won't be executed as an alias since it's not used directly.

EDIT: Sorry I was wrong about saying that variables cannot be executed as command.

If a variable was the command itself, it could be executed, but not when its value is used as an alias. In that case, the value as an alias can work individually, since it's like you've set an alise.
But an alias cannot be passed from a variable.

  • Thank you for your insight; however, I am still confused as to how to obtain the desired behaviour. How can I interpret the variable as if it were the command?
    – Cole C
    Jul 24 '16 at 21:30
  • @ColeC What do you exactly want as your result?
    – user172564
    Jul 24 '16 at 21:31
  • I want to execute the string stored in the variable as if it were the command.
    – Cole C
    Jul 24 '16 at 21:33
  • I have resolved my problem with the use of eval, which evaluates a string as a command.
    – Cole C
    Jul 24 '16 at 21:40
  • 1
    Hmm... filter='cat' followed by $filter file will cat the file, so you can use a variable as a command, it's just awkward to type on the command line. In a script it's a real option. Conditionally setting $filter to either cat or gzip makes switching a pipeline that puts $filter >$output at the end between compressing or not compressing a breeze.
    – Kusalananda
    Jul 24 '16 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.