mk.. mk.. - 1 year ago 67
Bash Question

In Linux shell scripts what does 'x=${1:3:1}' mean?

Inside the function of a shell script I see something like this

local x

What does
mean? I know that
are arguments of the function. So does the above statement mean that
x = $1:$2:$3

Also, it is really helpful if someone can suggest on how do I google search for speacial characters like this? Any standard keywords? I tried searching 'what is ":" in shell scripts' etc.. But the results are random when trying to search for special characters.

Answer Source

This is called parameter expansion in shell.


This one can expand only a part of a parameter's value, given a position to start and maybe a length. If LENGTH is omitted, the parameter will be expanded up to the end of the string. If LENGTH is negative, it's taken as a second offset into the string, counting from the end of the string.

OFFSET and LENGTH can be any arithmetic expression. The OFFSET starts at 0, not at 1.

e.g lets say the parameter is a string,

MYSTRING = "Be liberal in what you accept, and conservative in what you send"

echo ${MYSTRING:34:13}

The above will give you the following


as it will count the 33th(index start at 0) character which will start with the character "c" and then count (13 charcter) length .

So in your case $1 is the parameter you pass to your script and then it offsets 3 characters of that and take a string of length 1 and initialize it to x.

Read more here :

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download