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
2 answers

'Docker cp' with parameter expansion (PowerShell)

I want to copy a directory into a docker container, which is only defined by a certain query. I tried this: docker cp ../from-here/. $(docker ps -aqf "name=my-ending$"):/to-here Unforunately this throws this error: "docker cp" requires exactly 2…
0
votes
3 answers

Deleting leading spaces is not working in Bash

I have a string in Bash which may or may not start with any number of leading spaces, e.g. " foo bar baz" " foo bar baz" "foo bar baz" I want to delete the first instance of "foo" from the string, and any leading spaces (there may not be…
Lou
  • 2,200
  • 2
  • 33
  • 66
0
votes
3 answers

Bash math expression

I need help with this its been busting my mind. I have a read with a variable with integers 10 20 -30. All separated by white space. I try to change the minus to plus and save it onto another variable but it's not saving. If I can't change to plus I…
0
votes
1 answer

What is this bash parameter expansion syntax?

I've never seen this +x} syntax before and I can't find docs about it. What does it do? if [[ -z "${EMPLOYEE_CLUSTER+x}" ]]; then export EMPLOYEE_CLUSTER=shared fi
mattalxndr
  • 9,143
  • 8
  • 56
  • 87
0
votes
2 answers

How to convert day name to lowercase in bash script?

I usually use the following bash script to rename file with the days of the week (e.g., Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday): #get date export LANG=id_ID export TZ=Asia/Jakarta DAY=$(date --date='0 days'…
PUSTAKAKORAN.COM
  • 455
  • 1
  • 5
  • 18
0
votes
2 answers

What does ${i%.*} do in this context?

I'm learning a bit about running a bash script in a linux terminal, specifically in the context of converting audio video files. I came across this command here on SO that does exactly what I want. However, I'd like to understand it better: for i in…
rocksNwaves
  • 5,331
  • 4
  • 38
  • 77
0
votes
1 answer

Substitute a file extension in a zsh alias

I'm trying to create an alias for cwebp to run from zsh that converts an input image file, to an output image file of the same name, but with the .webp file extension: # in .zshrc alias cwebphoto='cwebp -preset "photo" -short -noalpha $1 -o…
s4l4x
  • 148
  • 6
0
votes
2 answers

Script runs when executed but fails when sourced

Original Title: Indirect parameter substitution breaks when the script is sourced (zsh) zsh 5.7.1 (x86_64-apple-darwin19.0) GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu) I’m developing a shell script on a Mac and I’m trying to keep it…
latentcode
  • 43
  • 6
0
votes
2 answers

How to extract last directory in path in Zsh?

Say I have a variable containing path to a directory (with a trailing slash): dir="path/to/dir/" How can I extract the last directory (i.e. "dir") using Zsh's parameter expansion? I would like to avoid calling external programs (like sed).
planetp
  • 14,248
  • 20
  • 86
  • 160
0
votes
2 answers

How to apply multiple parameter expansion in BASH for a single output?

files=("Benjamin Johnson" "Bastin Johnson" "Bagio Johnson") ( IFS=','; echo "${files[*]/#/Mr.}"; echo "${files[*]/ /_}" ) Expected Result Mr.Benjamin_Johnson,Mr.Bastin_Johnson,Mr.Bagio_Johnson Output result: Mr.Benjamin Johnson,Mr.Bagio…
0
votes
1 answer

How can I just extract one underbar-separated field from a filename?

I have a list of file names like this: REG_2016120200hourly_d01_20161202_00_00_00.nc Of this name I would like to extract and put in a variable: 1)date 20161202 for file in /path/*; do filename=$(basename -- "$file") …
onehalf
  • 47
  • 2
  • 9
0
votes
1 answer

printf interpretation of newline characters with parameter expansion string replacement

I have a string: string="foo=bar boo=far rab=oof raf=oob" I want to replace all white-space within the string with a newline character: string=${string// /$"\n"} When I use printf, bash prints: ~$ printf "%s"…
pntha
  • 39
  • 8
0
votes
2 answers

Change the base of an array elemnts from octal to decimal (inside a local bash script that run remote)

I have a problem in the bash script from below. I'm RUNNING THE CODE AS IT IS POSTED HERE Code of my bash script: #! /bin/bash CMD=' # go to a specific path set -x cd share/Images # create an array, perform the extraction of dates from folders names…
Mihai
  • 29
  • 8
0
votes
0 answers

Parameter Expansion Regex?

I have a question regarding parameter expansion in Groovy. I have a variable called : ${My_Version} now I have seen that you can only use the first 4 digits of the variable with the following possibilities : ${My_Version[0..4]}. Now I have the…
Martino
  • 1
  • 2
0
votes
1 answer

How to expand a parameter pack into a vector

Compile problem error: no matching constructor for initialization of 'std::vector' The code base I am building has several objects that do not need to be variadic template parameters. I wanted to make them accept a vector of std::any. My objects are…