Questions tagged [variable-expansion]

Evaluating or expanding a variable to get its value. Depending on the language, a variable may be expanded one or more times.

246 questions
5
votes
2 answers

Bash quoting of current path (pwd)

I have encountered a most annoying problem that occurs on the PWD variable when the current path includes a space. My code looks somewhat like this: mycommand |sed -E ' s|mystuff|replacement| ; s|'$(pwd)'|replacement| ; ' This works great,…
Ludvig A. Norin
  • 5,115
  • 7
  • 30
  • 34
5
votes
3 answers

Powershell variable expansion when calling other programs

I have a small problem trying to unzip a file using the 7za command-line utility in Powershell. I set the $zip_source variable to the zip file's path and the $unzip_destination to the desired output folder. However the command-line usage of 7za…
BergmannF
  • 9,727
  • 3
  • 37
  • 37
4
votes
1 answer

Use wildcard expansion to echo all variables in zsh

With multiple variables beginning with the same pattern can a wildcard be used to echo all matched patterns? when zzz1=test1; zzz_A=test2; zzza=test3 What is the best way to match all variables starting with zzz. Where something like echo $zzz* or…
Stuber
  • 447
  • 5
  • 16
4
votes
2 answers

Nested default arguments in bash?

Is it possible to do nested parameter expansion in bash? (e.g.: VAR=${{1:-$ENV_VAR}:-hard-coded default}) I want to set command line arguments with default values. However, before using a hard-coded default I would like to check for an environmental…
ruasoliveira
  • 192
  • 1
  • 11
4
votes
1 answer

Replace Last Occurrence of Substring in String (bash)

From the bash software manual: ${parameter/pattern/string} The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. ... If…
Jordan Mackie
  • 2,264
  • 4
  • 25
  • 45
4
votes
3 answers

In Bash, is there a way to expand variables twice in double quotes?

For debugging my scripts, I would like to add the internal variables $FUNCNAME and $LINENO at the beginning of each of my outputs, so I know what function and line number the output occurs on. foo(){ local bar="something" echo "$FUNCNAME…
4
votes
1 answer

Escape $ in variable passed to docker run command

I can't figure out how to escape the dollar sign in order to access a variable defined in an environment in docker, when executing the command from a makefile. This is a minimal representation of my setup and I'm trying to echo /mypath define…
Makers_F
  • 3,033
  • 4
  • 36
  • 51
4
votes
1 answer

Brace expansion with $@ arguments

Suppose I call a script with 3 arguments, a, abc, and xyz such that $@ contains these three arguments. Suppose I want to call write a command: command fooa fooabc fooxyz bara barabc barxyz How would I accomplish that? I don't think {foo,bar}$@ or…
arcyqwerty
  • 10,325
  • 4
  • 47
  • 84
4
votes
2 answers

Is there a way to prevent percent expansion of env variable in Windows command line?

I'm using the following git command in git bash on Windows: git log --format="%C(cyan)%cd%Creset %s" --date=short -5 It displays commit date (%cd) followed by commit message (%s). Commit date is wrapped with color markers: %C(cyan) to start colored…
jakub.g
  • 38,512
  • 12
  • 92
  • 130
4
votes
1 answer

Windows CMD Batch: FOR /R with DelayedExpansion

On my desktop, there is a folder named "test". Inside this folder is two files, "file1.txt" and "file2.txt". Take a look at this simple batch script: @ECHO OFF SET test="C:\Users\Tyler\Desktop\test" ECHO %test% FOR /R %test% %%F IN (*) DO ( …
TSmith
  • 405
  • 3
  • 10
4
votes
2 answers

Bash script: expansion of argument not using $@ or $*

Using $@ you can do things to a list of files in bash. Example: script.sh: #!/bin/bash list=$@ for file in $list; do _commands_; done Then i can call this program with ~/path/to/./script dir1/{subdir1/*.dat,subdir2/*} This argument will expand to…
Jonatan Öström
  • 2,428
  • 1
  • 16
  • 27
4
votes
1 answer

bash quotes in variable treated different when expanded to command

Explaining the question through examples... Demonstrates that the single-quotes after --chapters is gets escaped when the variable is expanded (I didn't expect this): prompt@ubuntu:/my/scripts$ cat test1.sh #!/bin/bash actions="--tags…
pjvleeuwen
  • 4,215
  • 1
  • 18
  • 31
3
votes
1 answer

Tcl: constructing list with literal `$` in values

I'm trying to construct a (Tcl/)Tk command (to be associated with a widget's -command), that contains a variable that must be expanded at runtime. In the original code this variable had a fixed name (so everything was simple and we used…
umläute
  • 28,885
  • 9
  • 68
  • 122
3
votes
1 answer

Equal/minus sign without a colon in a parameter expasion in bash

I found a snippet like this in a Bash script recently: $ echo ${A=3} Now, I know that ${A:=3} would set the variable A if A is "falsy", or ${A:-3} would return 3 if A is "falsy." I have never seen these similar expressions without the colon though,…
brandizzi
  • 26,083
  • 8
  • 103
  • 158
3
votes
2 answers

Appending newlines to a variable to be "echo"ed out

I've been working in Linux for the last 12 years, worked with Windows and command lines before that and have had to recently resurrect those batch file skills for a little easy to use / edit utility. However, I'm having some issues in finding out…
bnoeafk
  • 489
  • 4
  • 16
1 2
3
16 17