Remove last word in bash variable
bash
Solution
`args=${*:3}` flattens your argument list. You don't want to do that. Consider following the pattern given below instead:
# this next line sets "$@" for testing purposes; you don't need it in real life
set -- \
"first argument" \
"second argument" \
"third argument" \
"fourth argument" \
"fifth argument"
# trim the first two
args=( "${@:2}" )
# trim the last one
args=( "${args[@]:1:$(( ${#args[@]} - 2 ))}" )
# demonstrate the output content
printf '<%s>\n' "${args[@]}"
Running the above yields the following output:
<third argument>
<fourth argument>
...and, by doing so, demonstrates that it's correctly keeping arguments together, even when they contain spaces or wildcard characters.
For a shell completion script, you might also consider:
printf '%q ' "${args[@]}"
...which quotes content in such a way as to be eval'able by the shell.
Problem
I have something like that: ``` ... args=$* echo $args ... ``` result is ``` unusable1 unusable2 useful useful ... useful unusable3 ``` I need remove all "unusable" args. They always at first, second and last position. After some investigation i find `${*:3}` bash syntax. It help remove first two. ``` ... args=${*:3} echo $args ... ``` result is ``` useful useful ... useful unusable3 ``` But I can't find how to remove last word using same nice syntax.