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
0
votes
0 answers

What does :- do in bash script for parameters/arguments?

arg2=${arg1:-} What does this do to arg1 when assigned to arg2? I don't see any differences when printing both args
would_like_to_be_anon
  • 1,639
  • 2
  • 29
  • 47
0
votes
1 answer

Bash command on a variable

What does the following bash command do? "${ctp// /}" I want to check whether a variable is composed of an empty string or is all spaces, so that I can assign a default value.
Dilna
  • 405
  • 1
  • 6
0
votes
0 answers

What does the ending `+x` mean in this bash script?

What does the ending +x mean in the BUILD_ENV_IMAGE+x of below script? if [[ -z "${BUILD_ENV_IMAGE+x}" ]]; then BUILD_ENV_IMAGE="${CI_CONTAINERS_REGISTRY}/build-env:${CI_CONTAINERS_BASE_TAG}-gcc11" fi I am not sure if it is bash or sh specific.…
smwikipedia
  • 61,609
  • 92
  • 309
  • 482
0
votes
3 answers

bash read between two strings using parameter expansion expression

This is how my input string looks like: INPUT_STRING="{/p1/p2=grabthistext}" I want to print grabthistext from the INPUT_STRING. I tried echo "${INPUT_STRING##*=}" which prints grabthistext} How do I read only grabthistext using parameter expansion…
Asdfg
  • 11,362
  • 24
  • 98
  • 175
0
votes
1 answer

Expanding bash vars with spaces as arguments to bash function in scripts

Not critical - but I'm trying to get a deeper understanding of bash scripting and this is driving me crazy! My goal - in a bash script: Define a function that accepts arguments Set a bash variable (CMD) with the function name & arguments Execute it…
ChrisNY
  • 3,917
  • 4
  • 27
  • 31
0
votes
1 answer

How to expend a variable with a quote, on the quote?

Given the following string: toto="function(param" I want to get the substring function from the string above, in bash. I tried the following: echo "${toto%(}" Which gives: function(param However, with this example: echo "${toto%param}" I…
Itération 122442
  • 2,644
  • 2
  • 27
  • 73
0
votes
0 answers

Parameter extension for (non-parameter) variables?

I have used a FOR loop as a workaround in a batch script to pare a filename stored in a variable down to just its path: FOR %%f IN ("%f%") DO (SET "o=%tmpdir%\%%~nxf" & SET "d=%%~dpf") lib\JREPL.BAT "(SourcePath=\q)\.(\q)" "$1%d:$=$$%$2" /XSEQ /f…
0
votes
1 answer

extract hostname with parameter expansion in a single assignment

I am trying to get the hostname for a url. I am able to do it with 2 assignments but I want to do it in a single step. #!/usr/bin/env sh HELM_CHART=oci://foo.bar.io/ns/chart-name host=${HELM_CHART#*//} host=${host%%/*} echo "$host" #…
The Fool
  • 16,715
  • 5
  • 52
  • 86
0
votes
1 answer

what is the meaning of : ${CONTAINER_CLI:="docker"} in shell scripting

I am learning shell scripting and came across this line : ${CONTAINER_CLI:="docker"} can someone please explain me what this line do? what is the meaning of : here?
Alankrit010
  • 129
  • 1
  • 3
0
votes
0 answers

How to pass parameter expansions into qsub?

I'm trying to use qsub to submit multiple parallel jobs, but I'm running into trouble with passing parameter substitutions into qsub. I'm using the -V option, but it doesn't seem to recognize what ${variable} is. Here's some code I tried…
0
votes
0 answers

for loop for iterating over a list of string

I need to loop through a list of strings, and use each word as parameter for my other functions, PB_REGION_UPPER=NA,NASA for region in ${PB_REGION_UPPER//,/} do echo $region done I was expecting the result of this to be NA NASA so I can pass…
0
votes
1 answer

bash parameter-expansion: removing multiple occurences of the same character

It's GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu). I have variable with text in it. For example: var="this is a variable with some text :)" now I want to have var2 having the same content but multiple spaces replaced with…
0
votes
0 answers

Meaning of "+x" in shell parameter expansion [ -z ${FILE+x}]

I am reading some bash code and I can't figure out what the "+x" modifier does in the if condition below. I have read the doc about parameter expansion and about if [-z ..] but this +x perplexes me. In fact, there isn't a single occurrence of "+x"…
asachet
  • 6,620
  • 2
  • 30
  • 74
0
votes
1 answer

Bash parameter expansion: get entire remainder of a string after an offset

How can I avoid rest=${burger:1:99999999999999}, is there something that can replace the 9999999999 while still getting the remainder of the string burger ?
BLSPR
  • 567
  • 1
  • 3
  • 13
0
votes
2 answers

bash array slicing strange syntax in perl path: `${PATH:+:${PATH}}"`

On Linux Ubuntu, when you do sudo apt update && sudo apt install perl, it adds the following to the bottom of your ~/.bashrc file (at least, many months later, I think that is what added those lines): PATH="/home/gabriel/perl5/bin${PATH:+:${PATH}}";…
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265