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

Powershell equivalent of Bash "Alternate Value"

I'm trying to call a command in a Powershell script that has optional switches, and it must dynamically determine whether to include the switches. In Bash, you can do something like this (from this question): curl -o - ${PARAMS:+"--data"…
Jordan
  • 3,998
  • 9
  • 45
  • 81
1
vote
2 answers

Parameter Expansion in Zsh vs Bash: what is the equivalent of "${VAR,,}"?

I am trying to perform case modification with bash/zsh parameter expansion on macOS (11.4) and making some mistakes. Specifically, I want to take a variable that contains a string and turn it to snakecase (i.e.: from This is a STRING to…
baggiponte
  • 547
  • 6
  • 13
1
vote
0 answers

Extract the first part of of file name up to a pattern in Bash

This seems to be a common sort of problem, but I haven't seen an answer that works in Bash directly. What I've got so far, using the case where I want to grab the file name up to the first blank or period, where $f is the filename (e.g.,…
Gary Dale
  • 79
  • 9
1
vote
2 answers

Converting a BASH script to run on SH (via BusyBox)

I have an Asus router running a recent version of FreshTomato - that comes with BusyBox. I need to run a script that was made with BASH in mind - it is an adaptation of this script - but it fails to run with this error: line 41: syntax error: bad…
Silviu G
  • 1,241
  • 10
  • 31
1
vote
0 answers

bash script doesn't act like shell

Here is my script: #!/bin/bash x="aabcaa" y=${x##*(a)} echo $y It outputs aabcaa when I expect bcaa. When I try the same commands on a bash shell (got with bash command from zsh shell), everything works as I want. I checked the version with bash…
paulvand
  • 31
  • 1
1
vote
3 answers

create new directory with incremental version using bash

I need to generate new directories in incremental fashion. Below is the folder structure:- 1. Dockerfile 2. Makefile 3. manifests + 1.0.0 + 1.0.1 I want to fetch the latest version from the existing directories in a variable i.e.…
Rishab Prasad
  • 771
  • 1
  • 8
  • 21
1
vote
0 answers

zsh parameter expansion that filters out array elements that match a pattern

What zsh parameter expansion can filter out array elements that match a pattern (like what grep -v does)?
XDR
  • 4,070
  • 3
  • 30
  • 54
1
vote
1 answer

Concatenate multiple files based on similar ID in a loop

I have these files in the same…
Alex
  • 355
  • 1
  • 7
  • 20
1
vote
2 answers

How to store sed arguments in variable for parameter substitution in BASH

I need to store some sed expressions in a variable and substitute it in the sed command. myArgs="-e 's/foo/bar/' -e 's/foo2/bar2/'" So far these are not working: echo "foo" | sed ${myArgs} # sed: -e expression #1, char 1: unknown command:…
111
  • 1,788
  • 1
  • 23
  • 38
1
vote
1 answer

How can I suppress globbing in parameter expansion?

Is there a way to suppress globbing in parameter expansion? Consider the case of a file named literally *.xml in a directory among other xml files. Is there a way to do a parameter expansion ${param##*/} affecting only the file *.xml and not all the…
Roland
  • 7,525
  • 13
  • 61
  • 124
1
vote
3 answers

Bash - Expanding a variable into another variable name

Let's say I have these variables: VAR_A=resultA VAR_B=resultB X=A I want to get the value of VAR_A or VAR_B based on the value of X. This is working and gives resultA: VAR="VAR_$X" RESULT=${!VAR} My question is, is there a one-liner for this?…
scandel
  • 1,692
  • 3
  • 20
  • 39
1
vote
0 answers

Bash Array Subsets by Negative Length

In Bash, I can use the ${parameter:offset:length} notation to slice elements of an array, but I can't use it in all the same ways as I would when slicing a string. Specifically, I would like to use this syntax to print all but the last n elements of…
vintnes
  • 2,014
  • 7
  • 16
1
vote
3 answers

How to keep/remove numbers in a variable in shell?

I have a variable such as: disk=/dev/sda1 I want to extract: only the non numeric part (i.e. /dev/sda) only the numeric part (i.e. 1) I'm gonna use it in a script where I need the disk and the partition number. How can I do that in shell (bash…
Ben
  • 6,321
  • 9
  • 40
  • 76
1
vote
1 answer

Bash parameter expansion can't match spaces in variables like $* and $@

Using dash (0.5.10.2), I can do this: % dash $ set -- x hello world $ echo "<${*#x }>" This is the behavior I expect. The contents of $* (which are x hello world as assigned by set and delimited by spaces) are run through shell…
Adam Katz
  • 14,455
  • 5
  • 68
  • 83
1
vote
0 answers

Using Bash parameter expansion with Required check

I am trying to strip the string value in the variable and copy to another variable using bash parameter expansion. initialModified=${initial#abc} This give me the value in initialModified after stripping out abc from the beginning of the string in…
dreddy
  • 463
  • 1
  • 7
  • 21