Xaprb

Stay curious!

Bash parameter expansion cheatsheet

with 7 comments

Download Bash Cheatsheet

The bash shell has a powerful parameter expansion syntax, but it’s complex and hard to remember. I always forget which syntax does what, even though I use it all the time for scripts and one-off jobs. Unfortunately, I find the man page unhelpful as a reference, so I’ve made a cheat sheet for myself. Perhaps you’ll find it useful too.

If you want the whole story, type man bash into a terminal, and search for the section titled EXPANSION. There you’ll find the full details, such as this:

${parameter%word}
${parameter%%word}

The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the “%” case) or the longest matching pattern (the “%%” case) deleted. If parameter is @ or *, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

Hence the need for the cheatsheet :-)

Written by Xaprb

March 29th, 2007 at 6:29 pm

Posted in Uncategorized

7 Responses to 'Bash parameter expansion cheatsheet'

Subscribe to comments with RSS

  1. O’Reilly’s ‘Classic Shell Scripting’ (ISBN 0-596-00595-4) is a must if you want dozen of small tips like theses.

    This one is really neat too: http://thesmithfam.org/blog/2006/05/23/bash-socket-programming-with-devtcp-2/

  2. Thanks for the tip! I’m currently on an Ubuntu machine and /dev/tcp isn’t enabled so I can’t test it out, but that is neat!

    Xaprb

    1 Apr 07 at 9:11 am

  3. [...] Reference: Directly copied from here. [...]

  4. You have one not completely correct example in the pattern matching:

    # Oops, I didn’t mean that… get rid of the numbers.
    for file in *.gif; do mv $file ${file##[0-9]}; done

    – the ${file##[0-9]} by using ‘##’ should means to remove long matching of number – here that is unappliable:
    if $file is ‘a555name’ – nothing matched, as before number there is something else.
    if $file is ’123name’ – only first number is matched, while, I guess, assumed to remove ’123′: it is not happend, because [0-9] – is one number only.
    To remove ’123′ it could be ${file#*[0-9]} – no needs for ‘##’, as it would remove up to any found number in the file name, if the name has it.
    But it is not good enouth, as it, also, would remove ‘a555′ from the ‘a555name’, that is not planned.
    It should be specified as ‘any number of diggits from beggining of the variable value’, something like : /^[0-9]+/; but pattern expansion is not accepting such syntax.

    alex5161

    20 Dec 10 at 8:37 pm

  5. Using eval and echo you can kludge up nested parameter expansion.

    Let’s say you have $param1 and $param2. param1=”Hi” and param2=”There”. You have a routine you want to reuse, wherein all you have to pass it is 1 or 2 and it will use the appropriate $param1 or $param2.

    You set INDEX=1 or INDEX=2 depending on which one you want.

    > INDEX=1
    > echo $(eval echo $(eval ‘echo \$\{param$INDEX\}’))
    Hi
    > INDEX=2
    > echo $(eval echo $(eval ‘echo \$\{param$INDEX\}’))
    There

    It’s convoluted for sure, but it works.

    Keith T

    23 Feb 11 at 2:45 pm

  6. Important point to note is that $* and $@ bash parameters behave identical without double quotes but entirely different if used within double quotes. in case of $* individual parameters will be separated by IFS characters while in case of $@ individual parameters will appear in quotes and separated by space.

  7. You can break a lot of stuff if you don’t quote your expansions.

    http://mywiki.wooledge.org/BashGuide/Parameters#Parameter_Expansion

    Dave Eddy

    4 Jun 12 at 2:39 pm

Leave a Reply