Questions tagged [variable-substitution]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
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
18 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
30 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
49 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
55 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")[...
0
votes
1answer
23 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
60 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 ...
1
vote
1answer
71 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 "$(...
2
votes
2answers
64 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 ...
0
votes
3answers
75 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: ...
1
vote
1answer
33 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
41 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 ...
0
votes
0answers
41 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: ...
-2
votes
2answers
92 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 ...
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?
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 ...
1
vote
1answer
177 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
33 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.
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
62 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
151 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
112 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
485 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
830 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
75 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
257 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
94 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 ...
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
1k 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
297 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
828 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
43 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
754 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
37 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
269 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
828 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
207 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
1k 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
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
0answers
154 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
237 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
590 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
78 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
107 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
1k 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
92 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 ...