Differences between revisions 1 and 38 (spanning 37 versions)
Revision 1 as of 2007-05-03 00:09:05
Size: 2873
Editor: redondos
Comment:
Revision 38 as of 2016-05-02 05:57:06
Size: 374
Editor: GeneFriers
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq73)]]
== How can I use parameter expansion? How can I get substrings? How can I get a file without its extension, or get just a file's extension? ==

Parameter expansion is a separate section of the bash manpage ({{{man bash -P 'less -p "^ Parameter Expansion"'}}} or [http://tiswww.tis.case.edu/~chet/bash/bashref.html#SEC30 see the reference]). It can be hard to understand parameter expansion without actually using it. (DO NOT think about parameter expansion like a regex. It is different and distinct.)

Here's an example of how to use parameter expansion with something akin to a hostname (dot-separated components):

{{{
parameter result
----------- ------------------------------
${NAME} polish.ostrich.racing.champion
${NAME#*.} ostrich.racing.champion
${NAME##*.} champion
${NAME%%.*} polish
${NAME%.*} polish.ostrich.racing
}}}

And, here's an example of the parameter expansions for a typical filename.

{{{
parameter result
----------- --------------------------------------------------------
${FILE} /usr/share/java-1.4.2-sun/demo/applets/Clock/Clock.class
${FILE#*/} usr/share/java-1.4.2-sun/demo/applets/Clock/Clock.class
${FILE##*/} Clock.class
${FILE%%/*}
${FILE%/*} /usr/share/java-1.4.2-sun/demo/applets/Clock
}}}

You cannot nest parameter expansions. If you need to perform two separate expansions, use a temporary variable to hold the result of the first expansion.

You may find it helpful to associate that, on your keyboard, the "#" is to the left of the "$" symbol and the "%" symbol is to its right; this corresponds with their acting upon the left (beginning) and right (end) parts of the parameter.

Here are a few more examples (but ''please'' see the real documentation for a list of all the features!). I include these mostly so people won't break the wiki again, trying to add new questions that answer this stuff.

{{{
${string:2:1} # The third character of string (0, 1, 2 = third)
${string:1} # The string starting from the second character
  # Note: this is equivalent to ${string#?}
${string%?} # The string with its last character removed.
${string: -1} # The last character of string
${string:(-1)} # The last character of string, alternate syntax
  # Note: string:-1 means something entirely different.

${file%.mp3} # The filename without the .mp3 extension
  # Very useful in loops of the form: for file in *.mp3; do ...
${file%.*} # The filename without its extension (assuming there was
  # only one extension in the first place...).
${file%%.*} # The filename without all of its extensions
${file##*.} # The extension only.
}}}
25 year old Orthoptist Nickolas Colvin from Fort Severn, usually spends time with pastimes which include freshwater aquariums, Compte Brazzers gratuit and chess. Identified some amazing locales having spent 9 weeks at Surtsey.<<BR>><<BR>>
<<BR>><<BR>>
Also visit my blog post - [[http://www.comptebrazzersgratuit.fr|http://www.comptebrazzersgratuit.fr sponsor officiel]]

25 year old Orthoptist Nickolas Colvin from Fort Severn, usually spends time with pastimes which include freshwater aquariums, Compte Brazzers gratuit and chess. Identified some amazing locales having spent 9 weeks at Surtsey.



Also visit my blog post - http://www.comptebrazzersgratuit.fr sponsor officiel

BashFAQ/073 (last edited 2023-06-24 09:33:51 by emanuele6)