From d3253fdf76893819974d33b380b4dcd231c01a3d Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 7 Jan 2021 08:58:34 +0100 Subject: [PATCH] doc: $_ pitfalls --- doc/7_develop.md | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/doc/7_develop.md b/doc/7_develop.md index 40dd0c5..d3f3cb2 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -261,21 +261,32 @@ ADDME="$ADDME something to add" -> ADDME+=" something to add"" VAR="$(( 1 + 2 ))" -> (( var=1+2 )) INDEX="$(( ${INDEX} + 1 ))" -> (( INDEX++ )) +``` -# manipulate the same variable multiple times without storing intermediate -START="1a23_b__x###" +The special variable stores the expanded __last__ argument of the previous command. +This allows some useful optimisations, especially in combination with the no-op command `:`. -BAR="${START//[0-9]}" # a_b__x### -BAR="${BAR%%#*}" # a_b__x -BAR="${BAR/__x/_c}" # a_b_c +```bash +# mkdir plus cd to it +mkdir "tmpdir$$" && cd "$_" # rmpdir1234 (process id) -: "${START//[0-9]}" # a_b__x### +# manipulate a variable multiple times without storing intermediate results +start="1a23_b__x###" +... +: "${start//[0-9]}" # a_b__x### : "${_%%#*}" # a_b__x -BAR="${_/__x/_c}" # a_b_c +bar="${_/__x/_c}" # a_b_c -# works also for output of commands -: "$(date "+%A")" # Weekday -UPPERDAY="${_^^}" # WEEKDAY + +# BE AWARE OF ... +# pitfall/magic missing quotes: $_ is LAST arg +: ${SOMEVAR} # String in var $_ -> "var" +: $(<"file") # Content of\n file $_ -> "file" + +# pitfall/magic command substitution: globbing and IFS is applied! +: "$(echo "a* is born")"# $_ -> a* is globbed even quoted! +: "$(echo "a b c")" # $_ -> "a b c" +: "$(<"file")" # "Content of\n file" $_ -> "Content of file" ``` For more examples see [Pure bash bible](https://github.com/dylanaraps/pure-bash-bible) @@ -372,5 +383,5 @@ fi #### [Prev Function Reference](6_reference.md) -#### $$VERSION$$ v1.25-dev-30-gefca2e0 +#### $$VERSION$$ v1.25-dev-31-g6f8515d