Bash confusion about $1
For some reason, I can do this:
Code:
[1044:lthurber@tdsccas01 ~]$alias s='head -n 1 $1' Code:
[1047:lthurber@tdsccas01 ~]$ alias ht='echo "Head: "; head -n 1 $1' Code:
[1052:lthurber@tdsccas01 ~]$ alias ht='echo "Head: "; head -n 1 $1;' Code:
[1053:lthurber@tdsccas01 ~]$ ht testfile.txt Is there a way to work around this? I'd love to be able to get the first & last line of a file in one little command. Any help is appreciated, thanks! |
SWAG.......
Check to see if 'testfile.txt' is executable. |
I've tried your example aliases, and they work (except the one with the problem of course). This surprises me, because according to the bash man page you cannot use $1 in aliases. From "man bash":
Quote:
Create a script in your $PATH (e.g. /usr/local/bin) instead of an alias (or a function as the man page suggests). Use aliases only for simple one-liners. |
Nope, definitely not executable. I don't want it to be, all I want to do is get the first line out of it.
I'm just confused why this happens: [1027:lthurber@tdsccas01 ~]$ alias h1='head -n 1 $1' [1028:lthurber@tdsccas01 ~]$ alias h2='head -n 1 $1;' [1029:lthurber@tdsccas01 ~]$ h1 testfile.txt This is the first line of the file [1030:lthurber@tdsccas01 ~]$ h2 testfile.txt <<Ctrl+C>> bash: ./testfile.txt: Permission denied *shrug* I'm sort of mystified. Also, apologies for naming the thread wrong; that's what happens when I continue to diagnose during the writing of the thread :-[ |
Quote:
Thanks! |
Quote:
|
Unfortunately, it's not my server =/ It's alright, I made the script and then aliased 'ht' to it ;)
|
$1 wasn't being used as an argument in the examples you were giving.
What was happening in the examples you gave was that the shell was expanding $1 as a shell variable, not an argument. Most likely if you type `echo $1` it is empty. When you used the alias, it ran the echo, but it tried to run the next part as `head -n 1 ;` - that is head with no input. If you then forced it to continue it ran the last part (the file testfile.txt) which is not executable and you got the error 'Permission denied'. The man page recommends exactly what you ended up doing anyway - creating a script. Just my 2c on what happened during the execution... |
Quote:
Code:
alias s='head -n 1 $1' |
Yes it does. The steps are
1. You run `s somefile.txt` 2. The shell expands 's' to `head -n 1 $1` 3. The shell expands $1 to an empty string 4. The command that is run is head -n 1 somefile.txt The $1 is treated as an environment variable, not an argument. Try typing `echo $1` and see what is returned. |
Quote:
The filename is used by the (aliased) command because it's already there on the commmand line. Not because of expansion of "$1". Thanks. |
Glad it helped. It was the permission denied error that I recognised - I'd accidentally tried to run a non-executable file not long before I saw rose_bud4201's original post.
|
All times are GMT -5. The time now is 05:44 PM. |