ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
[1044:lthurber@tdsccas01 ~]$alias s='head -n 1 $1'
[1045:lthurber@tdsccas01 ~]$ s testfile.txt
This is the first line of the file
And I can do this:
Code:
[1047:lthurber@tdsccas01 ~]$ alias ht='echo "Head: "; head -n 1 $1'
[1048:lthurber@tdsccas01 ~]$ ht testfile.txt
Head:
This is the first line of the file
But I can't do this:
Code:
[1052:lthurber@tdsccas01 ~]$ alias ht='echo "Head: "; head -n 1 $1;'
When I try running it, I get "Head:" printed out, then the script hangs 'til I hit Ctrl+C, at which point I get:
As far as I can tell it's the ; after the head command that does it. Since I'm aiming for something along the lines of alias ht='echo "Head: "; head -n 1 $1; echo -e "\nTail: "; tail -n 1 $1', this is a bit of a problem.
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.
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:
There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used
I don't know how it's possible for it to work (sometimes), but I wouldn't count on it..
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 :-[
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.
Hmm..I guess I'll have to do it the script way then (I tend not to simply for fear of losing track of the scripts in the chaos that is my home directory).
$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...
$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.
Sounds true to me. But it does not explain why this does seem to work:
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.
3. The shell expands $1 to an empty string
4. The command that is run is head -n 1 somefile.txt
Ah! That makes sense.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.