Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [variable-substitution]

The tag has no usage guidance.

1
vote
0answers
27 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.
5
votes
5answers
597 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
48 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 > >(...
0
votes
0answers
48 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
0
votes
1answer
42 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
443 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 ...
0
votes
3answers
186 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
50 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
142 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
13 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
60 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
31 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 ...
5
votes
0answers
78 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
722 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 ...
3
votes
2answers
287 views

Bash parameter substituiton within commands

I have a passing understanding of Parameter Substitution, including substrings like so foo="Hello World"; greeting=${foo:0:6} But do I do this with commands? greeting="Hello" md5greeting=$(echo $...
3
votes
4answers
639 views

awk - replace number greater than 17 digits in a column with -

I have a CSV file containing timestamp values in UTC which I need to replace with -. There may be more than one timestamp in the same column, can you please let me know how do I do that? For example, ...
0
votes
4answers
41 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 ...
1
vote
1answer
240 views

Makefile: Copy using make variables --> error; Not so without variables!

In the following makefile InputLocation:=./Test OutputLocation:=$(InputLocation)/Output Input:=$(wildcard $(InputLocation)/*.md) Output:=$(patsubst $(InputLocation)/%, $(OutputLocation)/%, $(Input:...
0
votes
2answers
33 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/...
1
vote
3answers
126 views

Dynamic variables in shell

I have a following script that will take input (source path) from user and it will attach the volume inside docker container echo -n "Enter the source path: " read path docker run -v $path:/opt/$...
1
vote
1answer
418 views

Glob character within variable expands in bash but not zsh

I'm seeing an issue with zsh where a glob character within a variable is not expanding as I would expect. The following example does a better job of explaining it. $ echo $0 -bash $ echo $HOME/...
0
votes
2answers
95 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
2answers
547 views

Passing an argument with double and single quotes to another call in bash [closed]

I have a call ./run.sh name --vars="service_name='someothername'" and I'm trying to call another program with exactly same --vars="service_name='someothername'" argument from run.sh, however when ...
13
votes
4answers
2k 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
0answers
115 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 ...
5
votes
3answers
236 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 ...
0
votes
2answers
336 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
2
votes
0answers
66 views

What are the contexts where Bash doesn't perform word splitting and globbing?

Bash doesn't perform word splitting in globbing in these cases: LHS or RHS of an assignment, except for indexed arrays var=$value # simple variable declare -A hash key=...
0
votes
2answers
72 views

for loop variable assignment with whitespaces

Im collecting the LPAR names and assigning to a variable on AIX , Then using multiple commands for processing. LPAR Names on the system are : ABCD56777 TSM Mobile CD CPT 2 for lparname in `...
7
votes
2answers
930 views

calling other variables in a variable name (Bash)

At the moment I have the next paths langs="EN GE" dir_EN=/xx dir_GE=/zz As you can see the variable $langs has all possible languages in a single array. I would like to save all those paths in a ...
0
votes
2answers
59 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
3answers
35 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
170 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 ...
3
votes
2answers
75 views

Using variable values defined in one file, in files in the same directory

In this GitHub repository I have a directory named nwsm. This directory contains the file nwsm.sh that contains a master script (a script that runs other scripts). The directory also contains a few ...
1
vote
1answer
182 views

Basic usage of /etc/cron/ (d): correct pattern for /etc/cron (daily/weekly/monthly)

I use Ubuntu 16.04 with Bash and I've created this extensionless, shebangless file /etc/cron.daily/cron_daily: for dir in "$drt"/*/; do if pushd "$dir"; then wp plugin update --all --allow-root wp ...
0
votes
0answers
40 views

Variable expansion succeeds with echo but fails without a command (execution by expansion) [duplicate]

I created a file nano ~/uncwe/configurations/variables.sh. This file contains: set -a rse="~/${repo}/internal_software/rse.sh" set +a I sourced the file, then tried to execute: ${rse} bash ${rse} ...
1
vote
3answers
282 views

Trivial rm -rf command destroys my operating system in a testing machine

I use Bash 4.3.48(1) and I ran the following command pattern on a testing VPS machine: rm -rf ${drt}/${pma}* This command deleted the entire operating system (Ubuntu). That was evident by executing ...
3
votes
2answers
275 views

cat-redirect a file, but with variable expansions

I have the file ~/nginx_app containing this conf template: server { root /var/www/html/${domain}/; server_name ${domain} www.${domain}; } I also have this script: #!/bin/bash domain="$1" &...
0
votes
3answers
2k 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 "$...
9
votes
9answers
3k views

Bash: Extract one of the four sections of an IPv4 address

We can use the syntax ${var##pattern} and ${var%%pattern} to extract the last and first section of an IPv4 address: IP=109.96.77.15 echo IP: $IP echo 'Extract the first section using ${var%%pattern}: ...
4
votes
4answers
3k views

Backslash in variable substitution in Bash

I was working on a Bash script to help partition a hard drive correctly, and I came across a strange problem where I had to append a number to a variable. It took me a while to get the outcome right ...
3
votes
1answer
758 views

Double Quotes in Bash's variable substitution

I am trying to configure some software packages with a script thereby I got the following problem. Assume that the environment variable PREFIX is set to the location where I plan to install the ...
4
votes
2answers
882 views

How does Bash handle quoting in the string replacement section of parameter expansion?

Is there any consistent logic to it? some-command "${somevariable//some pattern/'how does this get parsed?'}" I've posted some conclusions and raw tests below as an "answer" but they're not a full ...
1
vote
1answer
388 views

Run cd with path variable in bash script not working [duplicate]

When i run the script: #!/bin/bash DRUPAL_ROOT=$(drush status root --format=list) if [ -z $DRUPAL_ROOT ] then echo -e "Not exists Drupal core" else echo $DRUPAL_ROOT cd $DRUPAL_ROOT fi Output: ...
-1
votes
1answer
514 views

Variable won't get expanded when INSIDE mysql shell

Before creating a new mysql user and DB, I did: read sps -s # Inputing and saving the sps (password) value. echo ${sps} # sps password value echoed. mysql -u root -p[PASSWORD] mysql>...
1
vote
3answers
92 views

How to dynamically create Bash code with a loop and execute it as it is created

I have a list of lines in a Bash script as follows if [ ! -z "$clone01" ]; then git clone "$clone01"; fi if [ ! -z "$clone02" ]; then git clone "$clone02"; fi if [ ! -z "$clone03" ]; then git clone "$...
2
votes
1answer
1k views

How can I change a Bash variable name in a loop and then expand the changed name?

I'm trying to expand variables Bash. I've searched but am not sure what the search terms are for what I am trying to do. Here is my code. Two questions: How do I get the desired output as shown? ...
7
votes
1answer
569 views

Word splitting in positional parameters

Consider the following example: IFS=: x="a :b" # three spaces echo ["$x"] # no word splitting # [a :b] # as is echo [$x] # word splitting # [a b] # four spaces Word splitting ...