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
2
votes
1 answer

How to replace each delimiter in an arbitrary number of command-line arguments that are all delimited name-value pairs?

How to change a command line argument in Bash? explains how to modify input arguments in bash. But in my case, I have a dynamic set of input arguments. I don't know how many are there. This is my command: send url key1=value1 key2=value2…
2
votes
1 answer

How to use a string with arguments to call a script as bash would do it when interprets the command line?

The desired outcome Is there a way to use a string that contains the arguments to call a script? str_params="this/one 'that one' and \"yet another\"" The function below prints feedback on the stdout on how the arguments were received: display_args…
rellampec
  • 698
  • 6
  • 22
2
votes
1 answer

How to understand "${PATH:+:${PATH}}" in bash?

I am seeing the following code in a bash script: export PATH=/opt/rh/rh-python38/root/usr/local/bin:/opt/rh/rh-python38/root/usr/bin${PATH:+:${PATH}} I do not understand the last part, where ${PATH:+:${PATH}} is manipulated.
xis
  • 24,330
  • 9
  • 43
  • 59
2
votes
1 answer

In bash, replace each character of a string with a fixed character ("blank out" a string)

MWE: caption() { echo "$@" echo "$@" | sed 's/./-/g' # -> SC2001 } caption "$@" I'd like to use parameter expansion in order to get rid of the shellcheck error. The best idea I had was to use echo "${@//?/-}", but this does not replace…
Johannes
  • 2,901
  • 5
  • 30
  • 50
2
votes
1 answer

Why is ${1[@]} invalid, even though ${stringVar[@]} works otherwise?

function HelloWorld() { var1=$1 echo ${var1[@]} echo $1 echo ${1[@]} } HelloWorld "Hello World" echo ${var1[@]} will run without issues but echo ${1[@]} gives me 'bad substitution.' I am not understanding the difference between these two cmds if…
2
votes
2 answers

How to replace a variable's comma-separated string with whitespaces?

I have the following code at my docker-entrypoint script: if [[ -v SERVICE_NAMES ]]; then sudo sed -i -e "s#%SERVICE_NAMES%#$SERVICE_NAMES#g" /etc/someservice/someconfig.file else echo "SERVICE_NAMES variable not set, unable to write…
user16071428
2
votes
2 answers

semver increment in shell script

I am trying to implement a solution to increment a version. Here's what I have come up with: #!/bin/sh -x VAR=1.0.1 # retrieved from Gitlab API case $1 in patch) TAG=${VAR%.*}.$((${VAR##*.} + 1)) ;; major) TAG=$((${VAR%%.*} + 1)).0.0 …
hdhruna
  • 865
  • 6
  • 15
2
votes
1 answer

Combine expressions and parameter expansion in bash

Is it possible to combine parameter expansion with arithmetic expressions in bash? For example, could I do a one-liner to evaluate lineNum or numChar here? echo "Some lines here Here is another Oh look! Yet another" > $1 lineNum=$( grep -n -m1 'Oh…
2
votes
3 answers

How to use Shell Parameter Expansion in Perl?

I am trying to implement a Bash parameter expansion technique to replace the existing extension of a file in a Perl script. This is my bash code for replacing the file extension which is working fine: mv "$f" "${f%.*}.png" On the other hand, I am…
2
votes
2 answers

Why does ${##parameter} always return 0?

Suppose that parameter=value is an internal variable in bash. When I try ${#parameter} in bash it returns a 5. But when I try, for instance, ${##parameter} or ${###parameter} it always give back 0 in return. Why it does not say that it is a…
Glitch
  • 155
  • 1
  • 9
2
votes
1 answer

what's the meaning of "${0##*[\\/]}" in shell?

I got the question when i looking other's shell script. I saw that declared APP_NAME="${0##*[\\/]}" Since I can't find any answer, what's the meaning of this code?
2
votes
1 answer

Name for ${...} constructs (for strings and arrays) in bash?

Bash allows things like ${#string} (string length) or ${array[10]} (indexing array). There's many more forms than the above, for example ones for trimming, replacing, changing case, etc. I've been unable to find a proper name for these. I've seen…
CoffeeTableEspresso
  • 2,614
  • 1
  • 12
  • 30
2
votes
1 answer

Nested parameter expansion not working as expected

zsh says: ${name} The value, if any, of the parameter name is substituted. It then also says: If a ${...} type parameter expression or a $(...) type command substitution is used in place of name above, it is expanded first and the result is used…
2
votes
0 answers

bash: Parameter Expansion, Use Default Values: Should word be quoted when the expansion is already quoted?

In a script, I am passing variables to an assembler (NASM), and I want these to be handled as strings by the assembler. That means the program being executed should receive pairs of quotes around literal text. (To ensure handling as text, in some…
ecm
  • 2,583
  • 4
  • 21
  • 29
2
votes
1 answer

Why does substring extraction extract entire words instead of characters?

Given a file test.txt, with the content This is a test. I want to extract a substring of its content, more specifically the second and third character ( hi ), using the command echo ${$(cat test.txt):1:2} However, this outputs the second and third…
flackbash
  • 488
  • 2
  • 16
1 2
3
11 12