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

Bash 4.4 prompt escape for number of jobs currently running

I stumbled across this post where user chepner proposed in his answer the usage of \j (as mentioned in the bash manual) to retrieve the current running count of background jobs. Basically it boils down to num_jobs="\j" echo ${num_jobs@P} Can anyone…
2
votes
6 answers

Extract substring from string after third last occurrence of underscore

I have a string in Linux shell. This string contains underscores in it. I want to extract a substring from the string. I want to extract the substring after the third occurrence of an underscore, counted from the end of the…
nmr
  • 605
  • 6
  • 20
2
votes
0 answers

Why parameter expansion does not work in Makefile?

I have next rule in Makefile: dbrestoretable: echo ${TABLE:-asdf} When I run: $ make dbrestoretable echo But: $ echo ${TABLE:-asdf} asdf Why default value asdf is not echoed in first case? UPD Specified duplicate speaks about how to use…
Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158
1
vote
1 answer

Change Filename in Pipe

I have a for loop that accepts a file with one type of extension, but the final command in the pipe requires the STDIN file to have an extension of another type. In my pipe I use awk to change the file type so that it is properly formatted for the…
JVGen
  • 401
  • 3
  • 10
1
vote
2 answers

How to pass a Bash command to `entr`, quoting to guard against filenames with spaces?

My Goal I'm writing a small Bash script, which uses entr, which is a utility to re-run arbitrary commands when it detects file-system events. My immediate goal is to pass entr a command which converts a given markdown file to HTML. entr will run…
Jonathan Hartley
  • 15,462
  • 9
  • 79
  • 80
1
vote
2 answers

Understanding the final `echo` in IFS=';' read -sdR -p $'\E[6n' ROW COL; echo ${ROW#*[}; in order to access current cursor height

I've been reading through the parameter expansion portion of the GNU bash manual guide and I can't seem to understand what is going on in the final echo command if the titular script (IFS=';' read -sdR -p $'\E[6n' ROW COL; echo ${ROW#*[};). I…
z.karl
  • 295
  • 2
  • 12
1
vote
1 answer

Bash param expansion "${var##pat}" behaves differently in script than in shell

I'm trying to use a pretty basic Bash parameter expansion in a script and it's not working; it works fine when I run it in my interactive shell, though. Here's the little test script: <~> $ cat /tmp/foo #!/usr/bin/env bash foo="bar 1.2.3" echo…
Joe Casadonte
  • 15,888
  • 11
  • 45
  • 57
1
vote
2 answers

How does ANSI C-Quoting in Herestrings work?

Why is this not working? bla=" multi line string " cat -A <
Booker B
  • 131
  • 2
  • 9
1
vote
1 answer

Why isn't this Bash string substitution working?

I'm trying to do a string substitution in bash to escape the dots in a version number to ultimately pass to grep. When I run echo ${3.9.1//./\\.} Expected output is 3\.9\.1. I get a bad substitution error instead. I don't understand how this isn't…
1
vote
0 answers

zsh: ${(f)...} doesn't split when used with assignment

It seems that when used in assignments, splitting with ${(f)...} does only work when using ${var=...} (in which case the result is joined again using the first character of $IFS), but not with regular assignments: % zsh -c 'IFS=:$IFS; printf ">…
1
vote
2 answers

Nested double quotes in parameter expansion

I was surprised that the following is a valid Parameter Expansion. Notice there are unescaped double quotes within double quotes: result="${var1#"$var2"}" Can someone please parse this for me?
Roland
  • 7,525
  • 13
  • 61
  • 124
1
vote
2 answers

Parameter expansion with replacement, avoid additional variable

I'm trying to join input $* which is one parameter consisting of all the parameters added together. This works. #!/bin/bash foo() { params="${*}" echo "${params//[[:space:]]/-}" } foo 1 2 3 4 1-2-3-4 However, is it possible to skip the…
Filip Seman
  • 1,252
  • 2
  • 15
  • 22
1
vote
1 answer

zsh ${var##$pat} parameter expansion with pattern var containing globs

It seems zsh doesn't honor globs inside variable patterns, in ${var##$pat} parameter expansions: $ zsh -c 'pat=/*; var=/etc/; echo "$var $pat"; echo "${var##$pat}"' /etc/ /* /etc/ # sh result: empty However, if $pat does not contain *, zsh and sh…
usretc
  • 723
  • 4
  • 9
1
vote
3 answers

Trimming whitespace from the ends of a string in Zsh

How does one remove any and all whitespace from the ends of a string in Zsh without spawning another process? After looking at the documentation for expansion in Zsh (namely sections 14.3 Parameter Expansion and 14.8.1 Glob Operators)—also viewable…
Tyler Crompton
  • 12,284
  • 14
  • 65
  • 94
1
vote
2 answers

How can I add a default parameter to a `Bash` function?

Most of the time when I am in some directory and running some job from the command line, I like to do: ls -ltr | tail -3 to confirm that the file I'm expecting is indeed there. To avoid typing it too often, I add to my ~/.bach_profile: alias…
Sam
  • 563
  • 5
  • 15