Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now

Questions tagged [variable-substitution]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1
vote
1answer
5k views

Variable Substitution in Awk Print Statement -v

I've spent over an hour poking at this. It can't be this hard... I want to print a column of data from one file to another file. The column I want to print is dependent on what is passed in and stuff ...
1
vote
1answer
50 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 ...
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 ...
1
vote
3answers
65 views

Use nested var for substitute var

I've a big question today ! I've made a little exemple of my situation. FOO="foo"; fooBAR="final"; var=${${FOO}BAR} echo $var I want to print "final". I tried all sort of things like ${${!FOO@}BAR} ...
1
vote
3answers
78 views

Use a list file to edit a master file through awk

everyone. I have two files: ports.lst and master.tbl ports.lst looks like this: hawaii-P1 hawaii-P2 hawaii-P3 losangeles-P1 losangeles-P3 master.tbl looks like this: #Site 1 Honolulu servername ...
1
vote
1answer
694 views

Error with find when using -newerct values stored in variables

I'm trying to create a bash function that will use a simple file such as sample.txt start=22 Mar 2016 10:00 end=22 Mar 2016 12:09 ...and find files that I have installed to /usr/local within the ...
1
vote
1answer
77 views

Where can I find documentation on shell script syntax for using a regex within a variable construct?

I'm debugging a shell script (which I didn't write) that contains this loop: read line < "$pid_file" for p in $line ; do [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p" done Can ...
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 "$(...
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
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.
1
vote
0answers
169 views

Bash variable value partially substituted in Curl input

Below is my shell script in which I'm trying to use the value of a variable as input to curl. However, the script is giving an error and when trying to debug the script, I can see that the value of ...
1
vote
2answers
596 views

Use sed command with variables

I have tried every possible solution available on the Stack Overflow and its related websites but did not find any solution. I spent a reasonable amount of time on this issue and am finally posting ...
1
vote
0answers
43 views

To divide a a loop variable per se and save in another one to be used later

I am facing a problem with bash script: I have a set of variables 'i' that ranges from 3.31 up to 3.35. My script prints out, as a function of the variable 'i' some input file for geometrical ...
1
vote
1answer
596 views

Replace command in Bash script from another script

I'm trying to setup a script that will fetch changes from github where a script has a command that's not executable in Mac OS X. What I'm aiming for is to use sed etc to replace the command within ...
1
vote
0answers
2k views

bash - expect and variables

I have the followin: while read line; do userinfo=$(echo $line | sed 's/@/,/') IFS=',' read -a address <<< "$userinfo" userid="${address[0]}" domain="${address[1]}" fname=...
0
votes
2answers
625 views

${p:2:1} Meaning in the shell script

Can anyone tell me the meaning of ${p:2:1} in shell scripting as in: var1="" for p in [1,2,3,4] do var1="${var1} ${p:2:1}" done
0
votes
2answers
359 views

What's the proper way to use a variable with spaces in part of a shell script command? [duplicate]

How can I include spaces as part of a variable used in an svn command for RHEL bash scripting? Or if there's something else wrong with the following, please advise. The SVN URL variable has no spaces,...
0
votes
3answers
85 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: ...
0
votes
2answers
540 views

what does it mean shell read command line arguments ${1,,}

In the Shell script code, the command line arguments assigned to variable like below. what does it mean comma(,) in the statement. What will be the difference when comma is added twice while reading ...
0
votes
1answer
607 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
2answers
290 views

Shell script parameter substitution

I have seen a line of shell script as follows: local soft_upgrade=${3:-false} What does this mean? I don't know what 3:-false means.
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
0
votes
2answers
89 views

How to leverage a user defined variable in a command to create a condition in an if [duplicate]

I have a script which takes input for two variables from users. Then, I try to use those variables in a command which is in turn being fed into a condition in an if. However, the user input value is ...
0
votes
3answers
52 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|...
0
votes
3answers
4k views

Reference bash variable from a variable [duplicate]

How can I reference a variable in bash based on another variable? Let me setup the example: package="foobar" # the variable I wish to reference is $foobar_darwin_amd64 # thus trying: echo "$...
0
votes
3answers
211 views

Confusion about bash command vs. variable substitution

I've been writing one-line bash scripts on the command line since 1989. These usually have the form: for name in 1 2 3; do wc $name.txt; done Now I'm trying to do some image manipulation with GIMP, ...
0
votes
2answers
3k views

How do I call a script with a wildcard pattern and two more arguments?

I have this code to rename a bunch of files: #!/bin/bash for file in $1 do mv -i "${file}" "${file/pattern/replacement}" done How do I replace "pattern" and "replacement" with $2 and ...
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
20 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
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 ...
0
votes
4answers
44 views

substitutions in vi editor

I have file called foo.txt. This file contains values: abc.tar.gz abc.1.1.tar.gz bca-1.2.tar.gz I would like to get an output like this abc abc.tar.gz abc.1.1 abc.1.1.tar.gz bca-1.2 bca-1.2.tar.gz ...
0
votes
2answers
103 views

Automating wp-config.php creation

I use Ubuntu 16.04 with Bash and I tried to run the following command set to automatically create a WordPress wp-config.php file with the WordPress Bash extension WP-CLI. Yet I'm having some problem I ...
0
votes
2answers
70 views

Reading output of command into variable in shell, splits text at every white space not just newline

I'm using sh (not bash/csh) on FreeBSD 11, and I don't understand this: In console Command: zpool status -v Result: pool: My_pool state: ONLINE status: One or more devices is currently being ...
0
votes
1answer
106 views

Why does parameter expansion happen before brace expansion in this example?

From https://unix.stackexchange.com/a/7739/674, why does parameter expansion happen before brace expansion in the following example? eval rm foo.{$ext0..$extN}
0
votes
2answers
2k views

Multiple variable expansion modifiers in the same expression

Why does the following idiom not work in bash 4.1.0? if [[ "${FUNCNAME[*]:1/$FUNCNAME/}" != "${FUNCNAME[*]:1}" ]] Here it is in context... function isCircularRef_test () { # ### Seems like this ...
0
votes
4answers
1k views

Bash Globbing Variable Substitution? [duplicate]

Possible Duplicate: Batch renaming files I want to rename files using their existing name as a base for the new one. So if I can ls these files with ls blue*+(.png) I'd want to rename them ...
0
votes
3answers
987 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
103 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 ...
0
votes
2answers
222 views

Using shell 'printf' where the format string is in a variable and doesn't have a fixed number of field placeholders?

I am writing a shell function which makes an external API call via cURL (the external API syntax isn't under my control). I've approached it like this (simplified): #!/bin/sh template_get_entry='...
0
votes
3answers
37 views

Variable in variable expansion

I want to evaluate a variable, whose name will need to dynamically evaluate another varuiable, i.e. the var ENVIRONMENT can be either prod, stg, or test, so I have 3 urls: URL_PROD=https://...
0
votes
1answer
260 views

Variable Expansion Adding Braces to Original String Value [closed]

NOTE: I missed a syntax issue in my script that was causing this. I was passing ${bar} as {$bar}. This can be removed or locked or whatever is done with these types of questions. I'm trying to test ...
0
votes
2answers
735 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 ...
0
votes
1answer
64 views

How to create variable from piped data

The command I using is: ls -l . | totLines=$(wc -l) echo $totLines . My goal is display the total number of lines, but by assigning the output of wc -l a variable name, then displaying that variable's ...
0
votes
1answer
784 views

Shell script that outputs chmod command for all files

On my EC2 Linux server, I blindly ran chmod 777 at directory that it turns out I should not have. There are hundreds of files in there, and I'm not sure which ones need to go back to their original ...
0
votes
1answer
176 views

How can I mix variables and wildcards in fish?

I have a value stored in a variable that I want to use as part of a wildcard, like this: set extension (somecommand) cp *$extension ~/filezone But the * prevents the variable from being dereferenced....
0
votes
1answer
350 views

Imagemagick having trouble escaping shell variables

I have an imagemagick command that is having trouble with the $quote$arcangle and $halfdimension$quote concatenated shell variables I'm passing as strings. Here's the transcript: $> echo convert ...
0
votes
1answer
569 views

Combining parameter expansion with modifiers in zsh

Say I have the following $f1=/some/path $f2=/some/subpath $f3=/some/other/subpath If I try: test1=${$f1/$f2/${f3:t}} zsh complains with bad substitution. However: test2=${f3:t} works well. What ...
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
1answer
268 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...?): ...
0
votes
2answers
42 views

How do I append a date to a file using Bash script with multiple variables?

My script is below, I can't remember how to make this make sense. #!/bin/bash input=$1 output=$2 cp $input $output_$(date +%m%d%y) The goal is to run the script as follows /path/script.sh /path/...