All Questions

Filter by
Sorted by
Tagged with
1
vote
1answer
32 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 ...
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
3answers
215 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/$...
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://...
9
votes
9answers
4k 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}: ...
-1
votes
1answer
723 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>...
2
votes
3answers
195 views

Variable Substitution

I have a variable assigned to a returned string: ytd_wk=$(cat file.csv | grep $(date +'%Y') | tail -1) I want to substring last 2 characters: ytd_wk=${ytd_wk:(-2)} Is there any way to use one-...
6
votes
3answers
517 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
3answers
260 views

bash - extglob 2nd “zero or more” operator not working

Given: $ shopt -s extglob $ TEST=" z abcdefg";echo ">>${TEST#*( )z*( )}<<" >> abcdefg<< Why is there a space before the letter 'a'? I would expect that the 2nd *( ) would ...
2
votes
4answers
425 views

I want echo the value in a1,a2,a3. But my code is printing only a1,a2 and a3 not the value stored inside them

a1="one" a2="two" a3="three" for ((i=1;i<=3;i=i+1)); do echo $a$i done I have already tried following echo ${a$i} echo $((ai)) echo ${a}${i} c=$(echo a$i) But none gave the right answer.
4
votes
2answers
144 views

Shell: why [ -n “${VAR-}” ] instead of [ -n “$VAR” ]?

I see a lot of shell scripts (for example, this one) checking for a variable's presence/absence like: [ -n "${VAR-}" ] As far as I can tell, using the ${VAR-fallback} form without providing a ...
-1
votes
1answer
511 views

Why is there a “Variable substitution” concept in Bash?

This question is very basic and I ask it not only for myself but for more newcomers who see the term "Variable substitution" and having the following thoughts: As far as I understand, the term ...
12
votes
3answers
1k views

Is “${PS1-}” valid syntax and how does it differ from plain “$PS1”?

I am looking at a script that has: if [ "${PS1-}" ]; then That trailing - bugs me a bit because it doesn't seem to Posix or Bash standard syntax. It this some arcane syntax that has been around ...
2
votes
3answers
70 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.
4
votes
1answer
556 views

readlink literal vs variable

Using BASH: the answer is probably obvious but, not for me. > echo $PWD /root/fcm > readlink -f ~ /root > # but then with a variable or literal > a='~' > readlink -f $a /root/fcm/~ >...
1
vote
2answers
2k 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?
1
vote
2answers
194 views

Why a value of a variable doesn't change inside backticks?

Here's my test code: a=1 echo $a echo `let ++a` echo $a The output that I see is 1, 1. Why doesn't the third line modify the value of a?
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 ...
3
votes
2answers
1k views

Default assignment to a null variable with command substitution

I'm trying to use the syntax: A=${B:-C} where A is the variable, B is the value I'm trying to assign, C is the default value if B is null. Now I want to replace B with the command nc -l 443, so if ...
2
votes
2answers
979 views

Defer variable expansion to subshell

My service uses environment variables extensively for configuration (mostly because it's really easy to inject those into a Docker container). I also have some maintenance scripts that I can run ...
6
votes
1answer
975 views

Variable with colon dash ${VAR:-}

How does "${2:-}" work in an 'if-then-else example' below? Somehow I cannot help but think that being ${2:-} it means the second argument, but I am curious what the colon(:) and dash(-) after the ...
1
vote
1answer
396 views

Creating a complex variable content by hand

I am creating a shell script dynamically and sending it to a remote server for execution. All is working except when I try to inject a variable with content coming from a file with multiple lines, ...
16
votes
2answers
9k views

How to avoid space after bash variable in string?

I have a variable for color. I use it to set color to strings, by evaluating it inside the string. However, I need to include space after the name(so that the name doesn't contain part of the text). ...
0
votes
2answers
615 views

/usr/bin/find invocation doesn't work when used with command substitution [duplicate]

In a script, I have: CMD='/usr/bin/find /etc -type f -exec /usr/bin/md5sum {} \; ' MD5=$("${CMD}") But the script gives the error: -bash: /usr/bin/find /etc -type f -exec /usr/bin/md5sum {} \; : ...
1
vote
1answer
253 views

Bash: How to get a value from an array to create command from it

Is it possible to use bash to fetch a string from an array provided in the following syntax within a config file? | | Version | PackageManager | Webserver | Database | |--------|---------|-----...
5
votes
2answers
754 views

How can I add datetime to an existing file name in Linux?

I want to add date and time to the file name, for example: 08032016out.log.zip This is what I try to do: _now=$(date +"%m_%d_%Y") _file="$_nowout.log" touch $_file.txt # creating new file with the ...
23
votes
2answers
5k views

What does ${PATH:+:${PATH}} mean?

I recently noticed the following in my cygwin profile, more precisely: /usr/local/bin:/usr/bin${PATH:+:${PATH}} What does it mean? Why is not just $PATH? Is this an 'if $PATH exists then add :$PATH'?...
2
votes
2answers
46 views

rename of file extension doesnt work [closed]

I'm using code like the following to create a logfile using the shell's string manipulation tools: I have a file name and, in the case of an error, I need to create a log file with the err. Suppose ...
1
vote
1answer
4k views

Passing a variable argument to egrep in a bash script

I have a script, myScript, which is trying to egrep the script argument in a file. Somehow variable expansion isn't working properly with the egrep command. I believe I've isolated the problem ...
0
votes
1answer
1k views

Searching path as search pattern in grep command

I have saved the path in the variable . var="/home/myfolder/subfolder" Need to search whether the path in available in a text file using grep. grep -l '${var}' * >abc.txt I'm trying to list ...
6
votes
4answers
31k views

Is there any way to print value inside variable inside single quote?

Consider I've set variable site and needs to be printed by echo or printf, but If I use single quote to write something and want to use variable then how? Example: $ site=unix.stackexchange.com $ ...
4
votes
1answer
81 views

How to make sure the $var variable in expanded in “$varsomething”?

I'm trying to run something like this for n in 1 2 3 4 5 6 7 do run_this_command whatever.$nx$n.in done The files are named as whatever.1x1.in, whatever.2x2.in, whatever.3x3.in,...
2
votes
2answers
79 views

variable substitution in CLI and for loops

I am pretty sure this has been asked already in some form, I just cannot come up with a good find to search it. I want to have a script that do things N times and to which I can pass as a variable ...
3
votes
2answers
3k views

Print file name extension using -exec in find

I am playing around with -exec flag of find command. I am trying to use the flag to print the extension name of files, using a fairly new Linux distribution release. Starting simple, this works: ...
2
votes
3answers
2k views

Variable expansion in Bash

I tried the following commands variable='one|name' echo $variable The output is one|name whereas echo one|name gives an error No command 'name' found. This is reasonable because bash treats | as a ...
2
votes
2answers
2k views

Environment variable not expanded inside the command line argument

I have a file user-pid.out2 which has "usernumber" and "process id" as two columns. based on usernumber I want to find corresponding process id. the first two lines below does not show the output ...
1
vote
1answer
417 views

Unknown syntax in /etc/rc.d/init.d/functions

Looking at the file /etc/rc.d/init.d/functions, I can see the following is declaring a number of variables as local variables. However, why are the variables killlevel and pid_file have an equal sign ...
2
votes
1answer
4k views

What does the expression ${MYVAR:+-x} mean in bash?

In a bash script I cannot post here I see the following expression: ${MYVAR:+-x} I understand the meaning of expressions like ${MYVAR:+OTHERVAR} and ${MYVAR:-OTHERVAR}, but an expression with both ...
2
votes
1answer
4k views

bash: export: `--wait': not a valid identifier

When I run git-sh from my terminal it works fine. But I get the following warning. bash: export: `--wait': not a valid identifier What is this and how do I get rid of it?
11
votes
2answers
51k views

Passing a variable to sed [duplicate]

I cannot not use a shell variable in sed in the $NUMBER form. I have this line in my shell script: cat shared.txt sed 's/whatever1/$2 ... whatever2/' > shared2.txt The result in shared2.txt looks ...
3
votes
2answers
3k views

Trouble with mv and adding the date

I want to be able to move $oldfile to my backup folder and add the date to the file name. So I tried this... mv $oldfile /home/u0146121/backupfiles/$oldfile_$(date +%F-%T) This just gives me this ...
4
votes
1answer
11k views

when to use double quotes with a variable in shell script? [duplicate]

I am in a confusion with what is meant by the double quotes referring to a variable. For example following two scripts gives the same output. What is really meant by the double quotes? Script1 ...
4
votes
4answers
4k views

Can I reload variable in a watch command?

Part of my job involves some data handling. One of the tasks is to 'flatten' a set of directories (which we'll call Dir for now), and copy them to a new location called DirFlat. This can take a long ...
4
votes
1answer
218 views

Different behavior of $() and `` [duplicate]

% PATH="MYPATH" % VAR="PATH" % echo $(eval echo \$$VAR) MYPATH % echo `eval echo \$$VAR` 5707VAR ^^ This is the process number. I thought those two were exactly the same, but obviously there are ...
0
votes
2answers
289 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
2k views

Unable to loop through cURL command

I am able to execute individual cURL commands using, curl -u user:password -v -XPOST -H 'Content-type: text/xml' -d '<featureType><name>quadrella_indica</name></featureType>' ...
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 ...
1
vote
1answer
1k views

Shell variables not working?

I'm trying to store the date in a variable ($DATE) so I can use it later, but it doesn't seem to work. See anything wrong? DATE=$(date +"%Y-%m-%d %R") sudo mysqldump -u root -pnotgivingyoumypassword -...
3
votes
4answers
2k views

pure shell complex substitution in variable

Is there any way to substitute text in variables on several patterns at time or even using back reference? For example, I have FILE=filename.ext and I want to change it to filename_sometext.ext. But ...