Can somebody please explain the what below piece of shell script would be doing?
END_POS=$((${#column}-$COLON_INDEX))
In this context, it stands for the the length of the value of that variable:
$ v="hello"
$ echo ${#v}
5
$ v="bye"
$ echo ${#v}
3
So what does this command?
END_POS=$((${#column}-$COLON_INDEX))
It gets the length of the value in $column and substracts the value in $COLON_INDEX using the $(( )) syntax to perform arithmetic operations:
$ column="hello"
$ colon_index=2
$ r=$((${#column}-$colon_index)) # len("hello") - 2 = 5 - 2
$ echo $r
3
From Arithmetic expression:
(( )) without the leading $ is not a standard sh feature. It comes
from ksh and is only available in ksh, Bash and zsh. $(( ))
substitution is allowed in the POSIX shell. As one would expect, the
result of the arithmetic expression inside the $(( )) is substituted
into the original command. Like for parameter substitution, arithmetic
substitution is subject to word splitting so should be quoted to
prevent it when in list contexts.
All possible uses of # that I can think of:
It stands for the length of the variable's value or element in case of arrays:
I have echoed variable's value length, array length and array's 1st index element's length:
$ var="abcd"
$ echo "${#var}"
4
$ arr=('abcd' 'efg')
$ echo "${#arr[#]}"
2
$ echo "${#arr[1]}"
3
$
Also $# gives you the number of parameters passed to the script/function.
I'm wondering if this could be possible:
scriptname: testing
#! /bin/bash
i=2
arg=`echo "$"$i`
echo $arg #value should be the value of $2 and not just '$2' string
echo $2
exit 0
command: testing a b
output
$2
b
Is there a way to make the value of $arg equal to the value of $2 which is "b" instead of just displaying the string "$2" aside from just directly assigning the value of $2 to $arg, arg=$2?
Tried doing this arg=echo ${$i} but I get this error: testing: ${$i}: bad substitution
Thanks in advance
Yes; you can use indirect expansion, which looks like this:
i=2
arg="${!i}" # equivalent to: arg="$2"
See the fourth paragraph of ยง3.5.3 "Shell Parameter Expansion" in the Bash Reference Manual.
I have a simple script where the first argument is reserved for the filename, and all other optional arguments should be passed to other parts of the script.
Using Google I found this wiki, but it provided a literal example:
echo "${#: -1}"
I can't get anything else to work, like:
echo "${#:2,1}"
I get "Bad substitution" from the terminal.
What is the problem, and how can I process all but the first argument passed to a bash script?
Use this:
echo "${#:2}"
The following syntax:
echo "${*:2}"
would work as well, but is not recommended, because as #Gordon already explained, that using *, it runs all of the arguments together as a single argument with spaces, while # preserves the breaks between them (even if some of the arguments themselves contain spaces). It doesn't make the difference with echo, but it matters for many other commands.
If you want a solution that also works in /bin/sh try
first_arg="$1"
shift
echo First argument: "$first_arg"
echo Remaining arguments: "$#"
shift [n] shifts the positional parameters n times. A shift sets the value of $1 to the value of $2, the value of $2 to the value of $3, and so on, decreasing the value of $# by one.
Working in bash 4 or higher version:
#!/bin/bash
echo "$0"; #"bash"
bash --version; #"GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)"
In function:
echo $#; #"p1" "p2" "p3" "p4" "p5"
echo ${#: 0}; #"bash" "p1" "p2" "p3" "p4" "p5"
echo ${#: 1}; #"p1" "p2" "p3" "p4" "p5"
echo ${#: 2}; #"p2" "p3" "p4" "p5"
echo ${#: 2:1}; #"p2"
echo ${#: 2:2}; #"p2" "p3"
echo ${#: -2}; #"p4" "p5"
echo ${#: -2:1}; #"p4"
Notice the space between ':' and '-', otherwise it means different:
${var:-word} If var is null or unset,
word is substituted for var. The value of var does not change.
${var:+word} If var is set,
word is substituted for var. The value of var does not change.
Which is described in:Unix / Linux - Shell Substitution
http://wiki.bash-hackers.org/scripting/posparams
It explains the use of shift (if you want to discard the first N parameters) and then implementing Mass Usage
Came across this looking for something else.
While the post looks fairly old, the easiest solution in bash is illustrated below (at least bash 4) using set -- "${#:#}" where # is the starting number of the array element we want to preserve forward:
#!/bin/bash
someVar="${1}"
someOtherVar="${2}"
set -- "${#:3}"
input=${#}
[[ "${input[*],,}" == *"someword"* ]] && someNewVar="trigger"
echo -e "${someVar}\n${someOtherVar}\n${someNewVar}\n\n${#}"
Basically, the set -- "${#:3}" just pops off the first two elements in the array like perl's shift and preserves all remaining elements including the third. I suspect there's a way to pop off the last elements as well.
$1 is the first argument.
$# is all of them.
How can I find the last argument passed to a shell
script?
This is Bash-only:
echo "${#: -1}"
This is a bit of a hack:
for last; do true; done
echo $last
This one is also pretty portable (again, should work with bash, ksh and sh) and it doesn't shift the arguments, which could be nice.
It uses the fact that for implicitly loops over the arguments if you don't tell it what to loop over, and the fact that for loop variables aren't scoped: they keep the last value they were set to.
$ set quick brown fox jumps
$ echo ${*: -1:1} # last argument
jumps
$ echo ${*: -1} # or simply
jumps
$ echo ${*: -2:1} # next to last
fox
The space is necessary so that it doesn't get interpreted as a default value.
Note that this is bash-only.
The simplest answer for bash 3.0 or greater is
_last=${!#} # *indirect reference* to the $# variable
# or
_last=$BASH_ARGV # official built-in (but takes more typing :)
That's it.
$ cat lastarg
#!/bin/bash
# echo the last arg given:
_last=${!#}
echo $_last
_last=$BASH_ARGV
echo $_last
for x; do
echo $x
done
Output is:
$ lastarg 1 2 3 4 "5 6 7"
5 6 7
5 6 7
1
2
3
4
5 6 7
The following will work for you.
# is for array of arguments.
: means at
$# is the length of the array of arguments.
So the result is the last element:
${#:$#}
Example:
function afunction{
echo ${#:$#}
}
afunction -d -o local 50
#Outputs 50
Note that this is bash-only.
Use indexing combined with length of:
echo ${#:${##}}
Note that this is bash-only.
Found this when looking to separate the last argument from all the previous one(s).
Whilst some of the answers do get the last argument, they're not much help if you need all the other args as well. This works much better:
heads=${#:1:$#-1}
tail=${#:$#}
Note that this is bash-only.
This works in all POSIX-compatible shells:
eval last=\${$#}
Source: http://www.faqs.org/faqs/unix-faq/faq/part2/section-12.html
Here is mine solution:
pretty portable (all POSIX sh, bash, ksh, zsh) should work
does not shift original arguments (shifts a copy).
does not use evil eval
does not iterate through the whole list
does not use external tools
Code:
ntharg() {
shift $1
printf '%s\n' "$1"
}
LAST_ARG=`ntharg $# "$#"`
From oldest to newer solutions:
The most portable solution, even older sh (works with spaces and glob characters) (no loop, faster):
eval printf "'%s\n'" "\"\${$#}\""
Since version 2.01 of bash
$ set -- The quick brown fox jumps over the lazy dog
$ printf '%s\n' "${!#} ${#:(-1)} ${#: -1} ${#:~0} ${!#}"
dog dog dog dog dog
For ksh, zsh and bash:
$ printf '%s\n' "${#: -1} ${#:~0}" # the space beetwen `:`
# and `-1` is a must.
dog dog
And for "next to last":
$ printf '%s\n' "${#:~1:1}"
lazy
Using printf to workaround any issues with arguments that start with a dash (like -n).
For all shells and for older sh (works with spaces and glob characters) is:
$ set -- The quick brown fox jumps over the lazy dog "the * last argument"
$ eval printf "'%s\n'" "\"\${$#}\""
The last * argument
Or, if you want to set a last var:
$ eval last=\${$#}; printf '%s\n' "$last"
The last * argument
And for "next to last":
$ eval printf "'%s\n'" "\"\${$(($#-1))}\""
dog
If you are using Bash >= 3.0
echo ${BASH_ARGV[0]}
For bash, this comment suggested the very elegant:
echo "${#:$#}"
To silence shellcheck, use:
echo ${*:$#}
As a bonus, both also work in zsh.
shift `expr $# - 1`
echo "$1"
This shifts the arguments by the number of arguments minus 1, and returns the first (and only) remaining argument, which will be the last one.
I only tested in bash, but it should work in sh and ksh as well.
I found #AgileZebra's answer (plus #starfry's comment) the most useful, but it sets heads to a scalar. An array is probably more useful:
heads=( "${#: 1: $# - 1}" )
tail=${#:${##}}
Note that this is bash-only.
Edit: Removed unnecessary $(( )) according to #f-hauri's comment.
A solution using eval:
last=$(eval "echo \$$#")
echo $last
If you want to do it in a non-destructive way, one way is to pass all the arguments to a function and return the last one:
#!/bin/bash
last() {
if [[ $# -ne 0 ]] ; then
shift $(expr $# - 1)
echo "$1"
#else
#do something when no arguments
fi
}
lastvar=$(last "$#")
echo $lastvar
echo "$#"
pax> ./qq.sh 1 2 3 a b
b
1 2 3 a b
If you don't actually care about keeping the other arguments, you don't need it in a function but I have a hard time thinking of a situation where you would never want to keep the other arguments unless they've already been processed, in which case I'd use the process/shift/process/shift/... method of sequentially processing them.
I'm assuming here that you want to keep them because you haven't followed the sequential method. This method also handles the case where there's no arguments, returning "". You could easily adjust that behavior by inserting the commented-out else clause.
For tcsh:
set X = `echo $* | awk -F " " '{print $NF}'`
somecommand "$X"
I'm quite sure this would be a portable solution, except for the assignment.
After reading the answers above I wrote a Q&D shell script (should work on sh and bash) to run g++ on PGM.cpp to produce executable image PGM. It assumes that the last argument on the command line is the file name (.cpp is optional) and all other arguments are options.
#!/bin/sh
if [ $# -lt 1 ]
then
echo "Usage: `basename $0` [opt] pgm runs g++ to compile pgm[.cpp] into pgm"
exit 2
fi
OPT=
PGM=
# PGM is the last argument, all others are considered options
for F; do OPT="$OPT $PGM"; PGM=$F; done
DIR=`dirname $PGM`
PGM=`basename $PGM .cpp`
# put -o first so it can be overridden by -o specified in OPT
set -x
g++ -o $DIR/$PGM $OPT $DIR/$PGM.cpp
The following will set LAST to last argument without changing current environment:
LAST=$({
shift $(($#-1))
echo $1
})
echo $LAST
If other arguments are no longer needed and can be shifted it can be simplified to:
shift $(($#-1))
echo $1
For portability reasons following:
shift $(($#-1));
can be replaced with:
shift `expr $# - 1`
Replacing also $() with backquotes we get:
LAST=`{
shift \`expr $# - 1\`
echo $1
}`
echo $LAST
echo $argv[$#argv]
Now I just need to add some text because my answer was too short to post. I need to add more text to edit.
This is part of my copy function:
eval echo $(echo '$'"$#")
To use in scripts, do this:
a=$(eval echo $(echo '$'"$#"))
Explanation (most nested first):
$(echo '$'"$#") returns $[nr] where [nr] is the number of parameters. E.g. the string $123 (unexpanded).
echo $123 returns the value of 123rd parameter, when evaluated.
eval just expands $123 to the value of the parameter, e.g. last_arg. This is interpreted as a string and returned.
Works with Bash as of mid 2015.
To return the last argument of the most recently used command use the special parameter:
$_
In this instance it will work if it is used within the script before another command has been invoked.
#! /bin/sh
next=$1
while [ -n "${next}" ] ; do
last=$next
shift
next=$1
done
echo $last
Try the below script to find last argument
# cat arguments.sh
#!/bin/bash
if [ $# -eq 0 ]
then
echo "No Arguments supplied"
else
echo $* > .ags
sed -e 's/ /\n/g' .ags | tac | head -n1 > .ga
echo "Last Argument is: `cat .ga`"
fi
Output:
# ./arguments.sh
No Arguments supplied
# ./arguments.sh testing for the last argument value
Last Argument is: value
Thanks.
There is a much more concise way to do this. Arguments to a bash script can be brought into an array, which makes dealing with the elements much simpler. The script below will always print the last argument passed to a script.
argArray=( "$#" ) # Add all script arguments to argArray
arrayLength=${#argArray[#]} # Get the length of the array
lastArg=$((arrayLength - 1)) # Arrays are zero based, so last arg is -1
echo ${argArray[$lastArg]}
Sample output
$ ./lastarg.sh 1 2 buckle my shoe
shoe
Using parameter expansion (delete matched beginning):
args="$#"
last=${args##* }
It's also easy to get all before last:
prelast=${args% *}
$ echo "${*: -1}"
That will print the last argument
With GNU bash version >= 3.0:
num=$# # get number of arguments
echo "${!num}" # print last argument
Just use !$.
$ mkdir folder
$ cd !$ # will run: cd folder
Where am I going wrong?
I have some files as follows:
filename_tau.txt
filename_xhpl.txt
filename_fft.txt
filename_PMB_MPI.txt
filename_mpi_tile_io.txt
I pass tau, xhpl, fft, mpi_tile_io and PMB_MPI as positional parameters to script as follows:
./script.sh tau xhpl mpi_tile_io fft PMB_MPI
I want grep to search inside a loop, first searching tau, xhpl and so on..
point=$1 #initially points to first parameter
i="0"
while [$i -le 4]
do
grep "$str" ${filename}${point}.txt
i=$[$i+1]
point=$i #increment count to point to next positional parameter
done
Set up your for loop like this. With this syntax, the loop iterates over the positional parameters, assigning each one to 'point' in turn.
for point; do
grep "$str" ${filename}${point}.txt
done
There is more than one way to do this and, while I would use shift, here's another for variety. It uses Bash's indirection feature:
#!/bin/bash
for ((i=1; i<=$#; i++))
do
grep "$str" ${filename}${!i}.txt
done
One advantage to this method is that you could start and stop your loop anywhere. Assuming you've validated the range, you could do something like:
for ((i=2; i<=$# - 1; i++))
Also, if you want the last param: ${!#}
See here, you need shift to step through positional parameters.
Try something like this:
# Iterating through the provided arguments
for ARG in $*; do
if [ -f filename_$ARG.txt]; then
grep "$str" filename_$ARG.txt
fi
done
args=$#;args=${args// /,}
grep "foo" $(eval echo file{$args})