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
95
votes
4 answers

How can I execute a command stored in a variable?

What is the correct way to call some command stored in variable? Are there any differences between 1 and 2? #!/bin/sh cmd="ls -la $APPROOTDIR | grep exception" #1 $cmd #2 eval "$cmd"
Volodymyr Bezuglyy
  • 16,295
  • 33
  • 103
  • 133
18
votes
3 answers

Extract substring before dot

I try to substract the first string before a dot (.) in bash. For instance: 1.2.3 -> 1 11.4.1 -> 11 I used the following command based on the docs: s=4.5.0 echo "${s%.*}" But it ouptuts 4.5 instead of 4. I don't get it. Why is that?
Mornor
  • 3,471
  • 8
  • 31
  • 69
17
votes
2 answers

Bash: How to use operator parameter expansion ${parameter@operator}?

I've googled and tried so many things and never could get anything to work with ${parameter@operator}. All I find is more links to the same documentation. So I think a proper answer with practical examples would be very helpful to its…
Fireplume
  • 269
  • 2
  • 7
12
votes
3 answers

How to extract substring in Fish shell?

I got the following variable: set location "America/New_York" and want to display only the part before the / (slash) using fish shell syntax. Expected result America Bash equivalent Using bash, I was simply using a parameter…
Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178
10
votes
2 answers

Simple way to append to an environment variable that may not exist yet in csh

I am trying to append a path to the end of my PERL5LIB environment variable, but I am not sure that this variable will always exist. If it doesn't exist I would like to simply initialize it to the value that I am trying to append. Here is an example…
tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
10
votes
2 answers

Create URL friendly slug with pure bash?

I am after a pure bash solution to "slugify" a variable and that is not as ugly as mine. slugify: lowercased, shortened to 63 bytes, and with everything except 0-9 and a-z replaced with -. No leading / trailing -. A string suitable to use in URL…
Tom
  • 981
  • 11
  • 24
8
votes
2 answers

Parameter expansion with double caret ^^

In the following block of code, how are line# 3 and 4 evaluated? for f in "${CT_LIB_DIR}/scripts/build/debug/"*.sh; do _f="$(basename "${f}" .sh)" _f="${_f#???-}" __f="CT_DEBUG_${_f^^}" done
rubber duck
  • 83
  • 1
  • 4
7
votes
5 answers

How to remember which expansion ${var%} ${var#} work from which end?

I am having trouble remembering which one of the parameter expansions ${var%subst} or ${var#subst} remove from the front and which one from the back of the string. Example: $ var=/a/b/c $ echo dirname=${var#/*} filename=${var%%*/} dirname=a/b/c…
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
7
votes
2 answers

Regex matching end of a line $ not working in Bash Script

I'm trying to do a simple regex statement in a bash script that will match and substitute the end of a word. Below is what I'm trying to do. wordh > word:’ Below is the code I'm using. #!/bin/bash STAT=${STAT/h$/:’} I'm not familiar with bash…
user2743
  • 1,423
  • 3
  • 22
  • 34
6
votes
2 answers

Why does this parameter expansion replacement fail in bash 4.2 but work in 5.1?

I'm trying to port some code from bash 5.1 to 4.2.46. One function which tries to strip color codes from a specifically formatted string stopped working. This is a sample string text in such format. I turn on extended globbing for…
tomocafe
  • 1,425
  • 4
  • 20
  • 35
6
votes
1 answer

How does negative matching work in extglob in parameter expansion

Problem The behaviour of !(pattern-list) does not work the way I would expect when used in parameter expansion, specifically ${parameter/pattern/string} Input a="1 2 3 4 5 6 7 8 9 10" Test cases $ printf "%s\n"…
123
  • 10,778
  • 2
  • 22
  • 45
6
votes
2 answers

Shell parameter expansion on arrays

Say I read some data into a Bash array: $ IFS=" " read -a arr <<< "hello/how are/you iam/fine/yeah" Now, I want to print the first /-sliced field for each element in the array. What I do is to loop over the elements and use shell parameter…
fedorqui
  • 275,237
  • 103
  • 548
  • 598
5
votes
2 answers

parameter expansion using bang dollar (`!$`)

Is there any way to use !$ in a parameter expansion context? The desired usage that motivates this question is rapid (in terms of key strokes) alteration of the name of a file (e.g., instead of saving the file name in a variable and executing…
user001
  • 1,850
  • 4
  • 27
  • 42
5
votes
2 answers

Bash variable expansion with a '/'

Inside of a while read line loop, I see this variable expansion ${line/device name:}. I've tried running the script with my own input file and it just prints out the line. Can you tell me what that expansion is doing?
Jimbo
  • 51
  • 1
1
2 3
11 12