[LUNA] simple(ton?) bash question ....
Paul F. Pearson
pfpearson at mchsi.com
Fri Nov 21 14:31:29 CST 2014
"William A. Mahaffey III" <wam at hiwaay.net> wrote:
>
> Hmmmm .... I get:
>
> [root at Q6600:/etc, Tue Nov 18, 11:04 AM] 1085 # function lltr10 ()
> {lltr
> "$*" | tail -10}; export lltr10
> -bash: syntax error near unexpected token `{lltr'
> You have new mail in /var/spool/mail/root
> [root at Q6600:/etc, Fri Nov 21, 11:11 AM] 1086 # which lltr
> alias lltr='llt -r'
> [root at Q6600:/etc, Fri Nov 21, 11:11 AM] 1087 # function lltr10 () {
> lltr
> "$*" | tail -10 }; export lltr10
> > ^C
> [root at Q6600:/etc, Fri Nov 21, 11:12 AM] 1088 # lltr10 () { lltr "$*" |
>
> tail -10 }; export lltr10
> -bash: syntax error near unexpected token `('
> [root at Q6600:/etc, Fri Nov 21, 11:13 AM] 1089 #
>
>
> So the spaces around '{' & '}' are necessary (news to me), as
> (apparently) is the function keyword .... Why does it go into a prompt
>
> when I define & export the function ? My ultimate goal is to put this
>
> stuff into a bashrc script, either per user or global, which is why I
>
> ask .... Thanks & TIA ....
OK, here it is after I actually tried it (I don't have lltr as an alias, so I just used ls -ltr). This works with no reported errors.
$ lltr10() { ls -ltr "$*" | head -10; }
$ export lltr10
The semicolon is special, as you're specifying a list of commands (technically, Compound Commands). See [1], [2], and [3].
I *think* you're getting the '>' prompt and other errors because of the missing semicolon. I can't say for sure, because I don't know if you put it all on one line, or if email is word-wrapping for us. My examples are all on one line.
[1] http://www.gnu.org/software/bash/manual/bashref.html#Lists
[2] http://www.gnu.org/software/bash/manual/bashref.html#Shell-Functions
[3] http://www.gnu.org/software/bash/manual/bashref.html#Compound-Commands
------
Paul F. Pearson
We all laugh in the same language
More information about the LUNA
mailing list