Questions tagged [variable-substitution]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
2
votes
2answers
1k views

Behaviour of bash command substitution with command from string in variable

$ echo $(echo x; echo y) x y $ a='echo x; echo y' $ echo $($a) # expect 'x y' x; echo y Why does command substitution behave in such way? How to perform the command substitution for list of commands ...
0
votes
1answer
603 views

What is variable substitution in shell programming

This question is very basic and I ask it not only for myself but for other newcomers who see the term variable substitution and have the following thoughts: As far as I understand, the term variable ...
0
votes
1answer
38 views

Find: Substitute expression with a variable

$ find -name "Bik*" ./Biking ./Biking/Unsync/Biking.md.~6~ ./Biking/Rendered/Biking.gen.html ./Biking/Biking.md Now when I substitute expression with a variable as follows: $ a='-name "Bik*"' $ ...
0
votes
1answer
19 views

Umount: target is busy to variable

I want to check if a USB is busy and store the result in a variable: $ foo=$(umount /dev/sdb1) $ umount: /path/mounted: target is busy. But when I want to print $foo, I get an empty variable $ echo ...
0
votes
2answers
34 views

Copying lines from one file to another using sed and shell variables

I've been trying to figure out how to copy lines from one file to the end of another using sed with some shell variables. sed -n "$var1,$var2'p'" file1.txt > file2.txt I've tried that and many ...
0
votes
3answers
51 views

How do I join the results of a command substitution?

I want to create a list of mp3 files like a 1.mp3|a 2.mp3. I have tried echo ${(j:|:)$(echo *.mp3)} and some variations of it, but they don’t work: # too much splitting 01|The|Magic|Finger|01.mp3|...
2
votes
1answer
56 views

How to split an expression by space in bash?

I am getting the valid result I want in zsh but not bash, what am I missing? I am trying to split the string into words. Bash bash-3.2$ echo ${$(echo "Hello World")[@]} bash: ${$(echo "Hello World")[...
1
vote
1answer
79 views

How to debug this buggy variable assignment in zsh?

How can I find what’s causing this weird behavior? ~ mary@advv $ hsdhh() a=evil ~ mary@advv $ echo "$a" evil ~ mary@advv $ a=good ~ mary@advv $ echo "$a" evil Update: I have found that eval "$(...
0
votes
1answer
31 views

zsh: inconsistent error handling in assignments

This is zsh -f: Fereidoons-MacBook-Pro% local a=$(jaja) && echo bad zsh: command not found: jaja bad Fereidoons-MacBook-Pro% a=$(jaja) && echo bad zsh: command not found: jaja ...
-1
votes
2answers
66 views

Variable substitution from/to file [duplicate]

How do I replace placeholders in a file with variables from another file? (Like docker-compose is doing it.) I found many articles about single variable replacements or replacement by environment ...
0
votes
3answers
84 views

Can bash indirect expansion be used on a string rather than constructing a temporary variable?

Assuming we've all read https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html (specifically, search for indirect expansion). The question means, instead of doing: ...
2
votes
2answers
123 views

Read command in shell script - variable input substitution

I am new to Linux, pardon any wrong terminology used here. I am using Ubuntu 18.04. I am reading text in a shell script using read command. I have a variable in bash window that I want to substitute ...
1
vote
2answers
3k views

One line env variable set behavior [duplicate]

If I do this: SUMAN_DEBUG=foo echo $SUMAN_DEBUG I get no output but if I do: SUMAN_DEBUG=foo && echo $SUMAN_DEBUG then I get "foo" why is that?
13
votes
4answers
3k views

Is it possible to print the content of the content of a variable with shell script? (indirect referencing)

Let's suppose I've declared the following variables: $ var='$test' $ test="my string" If I print their contents I see the following: $ echo $var $test $ echo $test my string I'd like to find a ...
1
vote
1answer
35 views

Providing Bash substitution as a function argument

I have a following loop that works well: function uppercase_all_letters() { for filename in *; do echo "${filename^^}"; done; } How do I provide "${filename^^}" as a parameter to the ...
1
vote
1answer
49 views

Makefile commands with external args and internal variables

In a Makefile target I have a list of files that I want to split into x parts and work only with one part y, then pass those file names as argument to a test runner. I have no control over the ...
-2
votes
2answers
99 views

How to execute a command that has many nested single and double quotes with a quoted variable in bash

Here is my simple script #!/bin/sh thefile=/home/ark/arkserver/ShooterGame/Saved/SaveIsland/1288804998.arktribe while inotifywait "${thefile}" ; do a=\"`strings ${thefile} | tail -n 5 | head -n ...
0
votes
0answers
45 views

Why can't execute ls or cp commands with a variable as an argument [duplicate]

I am debugging a bash script. It executes cp and mv commands in the script when a variable containing a path is used as the argument to the command. I have duplicated it in the terminal as follows: ...
3
votes
1answer
1k views

error: Macro % has illegal name (%define)

I have a build support script for building my packages. Now I want to automatically set the Version in my spec files. I use the macro %{auto_version} for that. In my script I want to invoke rpmbuild -...
5
votes
1answer
1k views

Bash echo $-1 prints hb1. Why? [duplicate]

If I ask bash to echo the -1th argument it prints hb1: echo $-1 hb1 Why? What is it accessing?
6
votes
3answers
561 views

Why is there a number in the zsh parameter expansion ${1-$PWD}

I have this script I'm basing my current script off of. I just don't understand why he has typeset result part dir=${1-$PWD} in there. I get the same result if I just write dir=$PWD. With typeset is $...
3
votes
1answer
2k views

Is `echo $TEST` expanding an asterisk in the variable a bug? [duplicate]

Is this a Bash bug? $ mkdir test && cd test && echo "a" > "some.file" test$ echo '*' * test$ TEST=$(echo '*') test$ echo $TEST some.file Why is the second output the resolution of ...
27
votes
4answers
86k views

Use a variable reference “inside” another variable

I'm sure it is relatively simple, I just don't know how to do it. #!/usr/bin/ksh set `iostat` myvar=6 I want to something like echo ${$myvar} which i want interpreted as ${$myvar} -> ${6} -> value
1
vote
1answer
212 views

cp runs from command line but file not found from script [duplicate]

I have a script to reorganize my directory structure that contains: files=./*"$pattern"I{"$ifirst".."$ilast"}.ext cp ${files} ../"$pattern"/"$i"/ When I run the script, I get an error: ./*S6I{001.....
1
vote
0answers
34 views

!$ vs $_ difference? [duplicate]

In GNU Bash, both !$ and $_ expands to the last argument of the last command. I think they're identical and I need someone to tell me if there is any difference.
0
votes
2answers
1k views

Expect Scripts - Reading from Files and Accepting User Input

How can I write an "Expect" script to: iterate over a list of IP addresses and log in to each to perform commands (like in a BASH for loop). Right now I just copy/pasted the few lines that are my ...
8
votes
5answers
1k views

Using parameter substitution on a Bash array

I have file.txt that I need to read into a Bash array. Then I need to remove spaces, double quotes and all but the first comma in every entry. Here's how far I've gotten: $ cat file.txt 10,this 2 0 ...
2
votes
1answer
65 views

Combine brace and variable expansion in one line

I would like to have a logging function that takes filenames as arguments and replicate the stdout to all of these files. This is what I have come up so far: function logger() { exec > >(...
3
votes
1answer
764 views

Running a constructed command from bash script

I constructed a command like this in a bash script a="my_cmd"; a="$a --verbose"; echo $a; $a; It works and executes my command correctly. But when I add an environment variable to the mix it breaks. ...
0
votes
0answers
158 views

Use variable inside sed command [duplicate]

how can i use variables inside this sed command? sed -i.bak -e '/triggerword/,${w $DIR/tempfile' -e 'd;}' $DIR/targetfile inside -e '{w $DIR/tempfile' -e 'd;}' the $DIR/tempfile isn't recognized
1
vote
1answer
126 views

why curl can't expand variable inside the loop, but echo can?

I need to include variable inside curl for fname in "assets/*.drx"; do echo $fname curl -F file=@"$fname" "http://someurl.com/" echo $fnamename done Result: assets/...
10
votes
1answer
493 views

What does the substitution ${!var_name+x} mean?

I found a script that has a function that checks if a variable is set but i don't understand it very well. check_if_variable_is_set() { var_name=$1 if [ -z "${!var_name+x}" ]; then ...
8
votes
3answers
18k views

bash ${VAR//search/replace} and weird regex behaviour

I am trying to do some searching and replacing on a variable using the ${VAR//search/replace} parameter expansion. I have a pretty long and evil PS1, that I want to work out the size of after ...
0
votes
3answers
960 views

Modifying a shell variable with regex (bash)

I have a shell variable for example. a="big little man". How do I use regex in bash print out the variable with only the middle word capitalized? (big LITTLE man) I can do it by separating the ...
0
votes
2answers
726 views

Assign a variable to each line of the output and loop through it in bash

I have a command that will have multiple lines as output. i will need to use each line as a source to another Command to process it. i am trying to assign a variable and loop through it, but it fails ...
11
votes
2answers
5k views

Bash variable substitution of variable followed by underscore

The variable BUILDNUMBER is set to value 230. I expect 230_ to be printed for the command echo $BUILDNUMBER_ but the output is empty as shown below. # echo $BUILDNUMBER_ # echo $BUILDNUMBER 230
0
votes
2answers
81 views

How can I export a variable in bash where the variable name is comprised of two variables?

I'm trying to export variables inside of a for loop where I wanted each iteration of the loop to change what variable was being written to. I've been looking for the right syntax to satisfy exporting ...
0
votes
1answer
266 views

Bad substitution when running from cron

I have a function (notify_dba) that with two string args, a header and a body. (correction) Works well from shell prompt, but not in cron Err, doesn't work in either shell or cron (but should...?): ...
2
votes
1answer
26 views

How can I build an env file (with $ characters) listing as-is from echo command?

I am trying to build a file invoked from a script that echoes what is in quotes to a file - only, the $ gets omitted. This is what I was trying to do: echo -e "export ORACLE_BASE=/oracle/APP/grid \n ...
0
votes
0answers
14 views

Interaction of sed and for loop [duplicate]

for i in {1..25}; do cat file$((i)) | sed 's/.*/$((i))/'; done doesn't print the current value of i but just $((i)). How can I make it print the value of i instead?
0
votes
2answers
102 views

Running multiple command against files matching a brace+glob pattern without repeating it

I would like to run a series of commands against a set of files matched by a braces-and-globs pattern, without copy-pasting the pattern all over the place. I've been trying to do this by putting the ...
1
vote
1answer
35 views

Zsh: check existing of file before expanding

In Zsh parameter expansion, I have: "${test_var:-"${HOME}/test"}" but here I want to check if ${HOME}/test is exist or not, if not exist then test_var expand to " " (one space string). Is there an ...
2
votes
3answers
74 views

Adding in basic variable command

Commands are.. t=20 s=$t+30 I want the output of echo $s to be 50, but I cannot figure out how to write the s= command to get it to perform addition.
-1
votes
1answer
56 views

how generate and substitute files with sed [closed]

I need to change the seed's value, the output (HA.xyz) and the namefile to generate 100 different files. # MSA: H2O seed 129472 tolerance 1.0 filetype xyz output HA.xyz Structure H2O.xyz number ...
0
votes
1answer
46 views

Inserting variables in a file path on GNU linux (SLES 11)

I am tryin to insert 3 variables to build a path, but unable to get it . I am trying the below log_path="/vol02/logs/$dname/logs/103602_$msname/${msname}_start.log" dname=cfp msname=cfp003 i get ...
5
votes
0answers
88 views

Correct treatment of quotes in parameter expansions

Using the default value of IFS, consider unset var set -- ${var:-"hello"} echo "$#" This prints 1 as the parameter expansion expands to the single string hello. unset var set -- "${var:-"hello"}" ...
0
votes
1answer
29 views

Can variable-substitution-syntaxes be unified?

Can these two variable-substitution-syntaxes combined into one? rm "$s_a/$domain.conf" 2>/dev/null rm "$s_e/$domain.conf" 2>/dev/null like: rm "$s_{a,e}/$domain.conf" 2>/dev/null
1
vote
1answer
2k views

using variables inside the curl command not working in bash script

I am trying to write a bash script to update certain nodes in my repository. I wrote below script, but it does not seem to be working when I use variables inside curl. Below is the code. I tried all ...
5
votes
3answers
238 views

How to create a loop with five (5) input variables?

I have a proprietary command that requires five (5) inputs to properly execute the command. command -cval <value> -tval <value> -dval <value> -kval <value> I need to be able ...
25
votes
4answers
21k views

Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?

Why do I get different values for $x from the snippets below? #!/bin/bash x=1 echo fred > junk ; while read var ; do x=55 ; done < junk echo x=$x # x=55 .. I'd expect this result x=1 cat ...