LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-17-2012, 01:38 PM   #1
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Rep: Reputation: 24
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*
 
Old 03-17-2012, 02:02 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
1 members found this post helpful.
Old 03-17-2012, 02:07 PM   #3
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by colucix View Post
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.
 
Old 03-17-2012, 02:48 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Command line works, won't work from inside bash script w6lsn Programming 4 02-17-2011 05:45 AM
(BASH) Works on the command line, hangs as a script -- what's wrong? SilversleevesX Programming 17 08-08-2010 10:19 PM
Bash script works from command line, fails from cron cmfarley19 Linux - General 4 08-14-2009 12:24 PM
works on command line but not in bash script tara Linux - General 7 02-09-2009 03:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration