Questions tagged [variable-substitution]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0
votes
4answers
86 views

In bash: trailing space capture in variable substitution

I see this in BASH 4.3.48 (SLES12 SP4) and BASH 4.4.23 (OpenSUSE Leap 15.1) when trying to remove multiple trailing spaces from a variable's value: ~> xxx="-O -Wall " ~> echo "X${xxx%% }X" #...
0
votes
3answers
46 views

Why test -n returns true even when a variable is empty? What am I doing wrong? [duplicate]

according to the test(1) manpages: -n STRING the length of STRING is nonzero so I expected this to run fine: [ -n ${var} ] && echo "var is not empty" I used this ...
0
votes
1answer
28 views

User input to search bash array

folks-- I'm getting kinda stuck, here. I have a small script that will be incorporated into a larger script. This small piece is supposed to take a user input, and compare it to a list of stored ...
0
votes
2answers
85 views

Passing CFLAGS to configure via bash variable

Just when I think I know how the shell works fairly, something comes along and stumps me. The following commands were executed on GNU bash, version 3.2.25. I have several ./configure scripts that all ...
0
votes
0answers
28 views

Able to echo variable but unable to substitute value in java shell command

I am trying to read some variable (username and password) from config file and use them with java command:- abc.config user_id=myid user_password=mypassword jar=/home/abc.jar Now I am ...
2
votes
2answers
64 views

What is the right way to quote nested parameter expansions?

When dealing with with nested parameter expansions, which of these is the correct way to quote things? This one: var="${var#"${var%%[![:space:]]*}"}" Or this one: var="${var#${var%%[![:space:]]*}}"...
0
votes
0answers
18 views

exec bash one liner to avoid extra wrapper script [duplicate]

how come I can (simplified examples; might initially seem contrived) bash$ CMD='/bin/bash -c '\''ls'\''' bash$ exec $CMD or bash$ exec /bin/bash -c 'ls /tmp' but not bash$ CMD='/bin/bash -c '\''...
0
votes
1answer
48 views

bash native string manipulation displays jumbled output

I tried to use the bash string native manipulation for substituting string with my shell variables. var1='123' var2='2018-01-01' var3='2018-01-02' var4='myfunction('var1','var2','var3')' var5=${...
3
votes
1answer
82 views

Zsh parameter expansion

I tried to display the content of the variable fpath in column form using parameter expansion, and I couldn't do it. After some trying and web browsing I just found another solution; which does ...
1
vote
1answer
59 views

Substitute variable inside another variable in shell script without reassignment

I am trying to substitute a variable inside another variable in shell script. but its showing as empty string. Below is the scenario. Assigning "x" with string containing variable "abc" ~$ x="new ...
0
votes
1answer
42 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
43 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
128 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
59 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
65 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
51 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
223 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
122 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
2k 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
194 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
69 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
314 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
54 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
327 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
397 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
40 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
2k 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
69 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
181 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
167 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
550 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
2k 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
454 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
347 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
27 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
2answers
140 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
48 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
91 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
32 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
3k 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
315 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
1k 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
46 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
2k 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
68 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
472 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
1k 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
308 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='...

1
2 3 4 5