Questions tagged [parameter-expansion]

Parameter expansion replaces variables with their values as an evaluation phase of a command in most Bourne-derived shells.

Parameter expansion is one of the series of transformations that commands go through when evaluated in a shell.

Its main function is to interpolate parameters, a.k.a. shell variables, but it also allows for some manipulation. For example, in bash, assuming variable var contains the string 1234abcd:

  • $var expands to 1234abcd
  • ${#var} expands to the parameter's length, 8
  • ${var%cd} expands to the parameter tail-trimmed of cd: 1234ab

Section parameter expansion from bash's Reference Manual

167 questions
1
vote
1 answer

Reason for inconsistent behavior of single quotes during BASH parameter expansion?

When using BASH Parameter Expansion, string that variable expands into can be quoted/escaped, which works fine, except when the single quotes are used and the whole variable is escaped in double quotes: $ echo "${var:-\\a}" \a # ok $ echo…
Thunderbeef
  • 1,471
  • 1
  • 13
  • 17
1
vote
2 answers

Is this some kind of regex within bash variable instantiation?

What do those two assignations (i and C omitting the first one to void) do? Is it some kind of regex for the variable? I tried with bash, but so far there were no changes in the output of my strings after instantiating them with "${i//\\/\\\\}" or…
joscabmar
  • 13
  • 3
1
vote
1 answer

bash parameter expansion and combining pattern-matching operators

Is it possible to use parameter expansion to combine pattern-matching operators? For example, given the variable test=/home/archie/.vimrc.bak. I can delete the longest match from the beginning of $test with echo ${test##*/}: >> echo…
EarthIsHome
  • 655
  • 6
  • 18
1
vote
1 answer

bash echo - how to denote the end of command

NL=$'\n' CMD="" CMD="$CMD echo Hello ; $NL" CMD="$CMD echo World ; $NL" $CMD The above code gives the following output, the echo globing everything after it. Hello ; echo World ; Neither the new line character nor the semicolon does work here.…
1
vote
4 answers

Can snakemake config parameter values be strings with {} values to be interpolated/expanded?

Is there a way to define a snakemake config string in a .yaml file so that it can contain {wildcard} and {param} values, and when that string is used in a shell command, the {} values are substituted with the actual value of ""? For…
tedtoal
  • 1,030
  • 1
  • 10
  • 22
1
vote
1 answer

bash parameter substitute digit one or more times

I have a version string, for example version=1.2.23b2 I want to have only the last part with the b2, so I tried this: ${version##*[0-9]*.[0-9]*.[0-9]*} My thought was that * would mean no or many repetitions of the leading pattern, but in fact it…
Andreas Hubert
  • 381
  • 6
  • 18
1
vote
4 answers

Bash: Replace word with spaces equal to the length of the word

I thought my bash-fu was strong enough but apparently it isn't. I can't seem to figure this out. I would like to do something like this: var="XXXX This is a line" word_to_replace="XXXX" # ...do something echo "Done:${var}" Done: This…
lonetwin
  • 971
  • 10
  • 17
1
vote
1 answer

Shell parameter expansion: $# vs. ${#@}

Within a shell script, as far as I can tell, $# and ${#@} behave identically, both giving the number of positional parameters. Is there any difference between the two, and in what context would one be preferable over the other?
ivan
  • 6,032
  • 9
  • 42
  • 65
1
vote
1 answer

Zsh parameter expansion on output of command s flag doesn't work

In zsh, if I run: a=$(echo "foo, bar") echo ${a[(ws:, :)1]} I get foo as you would expect (w causes the index to refer to words s:, : makes , be the word separator). However, if I try to combine these: echo ${$(echo "foo, bar")[(ws:, :)1]} I get…
Radvendii
  • 41
  • 3
1
vote
2 answers

Can bash substring replacement use regexs?

Given a string such as string="val1 val2 val3 val4" how can you use bash substring replacement to remove a given substring and its adjoining space (which may or may not be present)? For example, this results in extra…
smeep
  • 332
  • 1
  • 17
1
vote
3 answers

Remove leading digits from a string with Bash using parameter expansion

The initial string is RU="903B/100ms" from which I wish to obtain B/100ms. Currently, I have written: #!/bin/bash RU="903B/100ms" RU=${RU#*[^0-9]} echo $RU which returns /100ms since the parameter expansion removes up to and including the first…
AASJC
  • 13
  • 2
1
vote
1 answer

Why bash indirect expansion has to use temp variable?

From https://stackoverflow.com/a/10820494/1764881, I know that the standard way of doing it seems to be: var="SAMPLE$i" echo ${!var} But, I can't seem to do any of these following forms. They all failed: echo ${!SAMPLE$i} echo…
Boyang
  • 2,520
  • 5
  • 31
  • 49
1
vote
2 answers

How to apply parameter expansion on brace expansion (range)?

I would like to apply parameter expansion (e.g. search and replace) in order to remove spaces from the brace expansion ({a..z}). It is possible? So I've the following range: {a..z} which consist all letters and I would like to remove spaces in one…
kenorb
  • 155,785
  • 88
  • 678
  • 743
1
vote
1 answer

Bash: using {} to replace filename extension

I'm testing out bash filename replacement with parameter expansion as shown here: How to change file extension in Linux shell script? However, I can't seem to get the following to work as I intend: given the following files: tmp/01. Losing A Whole…
EarthIsHome
  • 655
  • 6
  • 18
0
votes
1 answer

Why does the shell expand a quoted "$@" as multiple parameters

The following command prints foo bar, as expected: sh -c 'printf "$@"' -- "foo bar" However, when called with foo and bar as separate arguments, it only prints foo: sh -c 'printf "$@"' -- foo bar (sh and bash have the same behavior here) This…
ChrisB
  • 1,540
  • 6
  • 20