Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can parameter expansion be nested in Bash? [duplicate]

Tags:

Possible Duplicate:
Can ${var} parameter expansion expressions be nested in bash?

Is it possible to nest the shell parameter expansion (${})?

If I want to do something like this:

foo=( 1 2 3 4 5 )
echo ${${foo[@]/3/bar}:1}
like image 587
Tyilo Avatar asked Jul 17 '11 13:07

Tyilo


People also ask

What is parameter expansion bash?

'$' symbol is used for parameter expansion also that has various types of uses in bash. Parameter expansion can be used to modify, expand or replace the value of the parameter. The optional braces are used with the variable when using variable parameter expansion, such as 'echo ${myvar}'.

What is ${ 2 in bash?

It means "Use the second argument if the first is undefined or empty, else use the first". The form "${2-${1}}" (no ':') means "Use the second if the first is not defined (but if the first is defined as empty, use it)". Copy link CC BY-SA 2.5.

What is parameter substitution in bash?

The length in characters of the expanded value of parameter is substituted. If parameter is ' * ' or ' @ ', the value substituted is the number of positional parameters. If parameter is an array name subscripted by ' * ' or ' @ ', the value substituted is the number of elements in the array.

What character is often used in Bash to reference a variable?

You don't have to use any special character before the variable name at the time of setting value in BASH like other programming languages. But you have to use '$' symbol before the variable name when you want to read data from the variable.


1 Answers

No, you can't. (You can in zsh, but not in bash, ksh or other shells.)

You need to use an intermediate variable:

foo=( 1 2 3 4 5 )
tmp=("${foo[@]/3/bar}")
echo "${tmp[@]:1}"
like image 69
Gilles 'SO- stop being evil' Avatar answered Sep 18 '22 13:09

Gilles 'SO- stop being evil'