LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash: command works at command line, but not via an alias (https://www.linuxquestions.org/questions/programming-9/bash-command-works-at-command-line-but-not-via-an-alias-934969/)

porphyry5 03-17-2012 01:38 PM

bash: command works at command line, but not via an alias
 
Could someone kindly explain these results, why the command works at the command line, but fails when an alias is used.
Code:

~ $ alias fs
alias fs='ls -hsR /media/3tb|grep -i "$1"'
~ $ fs "wild at heart"
grep: wild at heart: No such file or directory
~ $ fs 'wild at heart'
grep: wild at heart: No such file or directory
~ $ ls -hsR /media/3tb|grep -i "wild at heart"
 933M DavidLynch-Wild At Heart.avi*
~ $ ls -hsR /media/3tb|grep -i 'wild at heart'
 933M DavidLynch-Wild At Heart.avi*


colucix 03-17-2012 02:02 PM

Aliases don't accept positional parameters (arguments). What you're really issuing is:
Code:

ls -hsR /media/3tb|grep -i "" "wild at heart"
hence the "No such file or directory" error. In other words, $1 is ignored and the empty string between double quotes is used as a pattern. You can try to write a shell function, instead. It is more versatile and accepts parameters and other goodies.

porphyry5 03-17-2012 02:07 PM

Quote:

Originally Posted by colucix (Post 4629268)
Aliases don't accept positional parameters (arguments). What you're really issuing is:
Code:

ls -hsR /media/3tb|grep -i "" "wild at heart"
hence the "No such file or directory" error. In other words, $1 is ignored and the empty string between double quotes is used as a pattern. You can try to write a shell function, instead. It is more versatile and accepts parameters and other goodies.

Thank you.

David the H. 03-17-2012 02:48 PM

To be completely clear, an alias is a simple command name substitution. If the first word of the line you run matches a defined alias, then it's replaced with the contents of the alias and the modified command line is executed.

Code:

$ alias echoit='echo foo'
$ echoit bar
foo bar

The command actually executed is "echo foo bar". "echoit" has been replaced with "echo foo".

In short, you can't build arbitrary commands with aliases, you can only use them to replace the first word with a different string. The rest of the contents of the line remain where they started.

Note that there are tricks you can use to do some really clever things with aliases, but it's generally not recommended to do so. aliases were really designed to be simple command shortcuts. Use functions for more complex stuff.


BTW, colucix's explanation is not quite right in one respect. Variable substitutions are still made. If the parent shell just happens to have a $1 value set, then it will show up in the final command.

Code:

$ alias echoit='echo $1'
$ echoit bar
bar                        #no output from $1
$ set -- foo                #set the first positional parameter in the shell
$ echoit bar
foo bar                        #now we get output.

But note that this $1 is the running shell's parameter, already sitting in the environment, and has nothing really to do with the contents of the command or the alias itself. The line as executed is still simply "echo $1 bar", with $1 being substituted before execution.


All times are GMT -5. The time now is 01:38 AM.