The tag has no usage guidance.

learn more… | top users | synonyms

2
votes
2answers
36 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
58 views

How to assign value to input variable in shell

I am asking user for input and taking input in the variables such as $1, $2 etc. I am checking the variable for null value and wants to replace the value if null. if [ "$2" == "" ]; then 2=value fi ...
7
votes
2answers
561 views

Grep to find the correct line, sed to change the contents, then putting it back into the original file?

I'm trying to change a single word on a specific line in a file, but I'm having some trouble connecting all together. Basically, on one line in my file there is a keyword 'firmware_revision', and on ...
12
votes
1answer
236 views

See the expansion of Bash variables for debugging?

Is there any tools/tricks available for expanding variables to make bash code easier to read? For example it would convert the following: DIR=/var/tmp FILE=${DIR}/file SCRIPT_ROOT=/opt/root TOOL=${...
3
votes
4answers
139 views

Assigning command to variable without alias

I just accidentally found out that in bash, we can assign a command into a variable without using alias. g=date $g Mon Jun 27 13:00:40 MYT 2016 That works. Here's another example: jj="ping yahoo....
1
vote
1answer
38 views

Possible to reuse first argument of BASH line in the same line?

I do this in BASH echo test "$1" ..expecting to get.. test test ..but I get.. test Is this something possible to do? It would make my life easier since having a list files I could do ...
1
vote
2answers
50 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 ...
0
votes
0answers
40 views

How to resolve a variable's value inside an array in bash

I have a few files whose names I have assigned into variables. (There may be n number of files). Those variables are passed into an array. Now I want to print the names of the original files using ...
0
votes
2answers
39 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,...
4
votes
2answers
54 views

Double variable substitution in bash

Program snippet: BASH_MIN_REQ="2.05" BINUTILS_MIN_REQ="2.12" BISON_MIN_REQ="1.875" BASH_CURR=$(bash --version | head -n1 | cut -d"(" -f1 | cut -d" " -f4) BINUTILS_CURR=$(ld --version | head -n1 | ...
2
votes
1answer
34 views

Variable with colon, dash in an if-then-else: `if [ “${2:-}” = “Y” ]` [duplicate]

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 ...
0
votes
1answer
41 views

Bash add text on line script [closed]

Trying to get my script to work but it won't work. Here is my script #!/bin/bash a="* * * * * /home/server/scripts/check.sh" b="0 06,18 * * * /home/server/scripts/checks2.sh" sudo sed -i '28s/^/'$b'\...
1
vote
1answer
53 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, ...
0
votes
1answer
40 views

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

From http://unix.stackexchange.com/a/7739/674, why does parameter expansion happen before brace expansion in the following example? eval rm foo.{$ext0..$extN}
7
votes
2answers
236 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
0answers
22 views

Automated port change

I want to Create a Bash file that a user can execute to change the port a service runs on. Essentially all I have to do is replace a number in a line of text with another number generated by a list. ...
0
votes
1answer
48 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
2answers
43 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 {} \; : ...
4
votes
1answer
33 views

Does tcsh have an equivalent for “${a:-b}” for default variable assignment like Bash?

I want to do this in one line because I want to use it in my aliases. Can I do this similarly in tcsh ? Using “${a:-b}” for variable assignment in scripts There are some ways of doing it here but ...
1
vote
1answer
45 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 | |--------|---------|-----...
1
vote
1answer
50 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 ...
2
votes
2answers
58 views

Multiple lines of input for multiple variables

I would like to enter multiple lines of values for 3 different variables. For example, lets say I have $a, $b, and $c in a line like: ADD $a in $b to get $C My input will be in rows for all 3 ...
0
votes
1answer
14 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 ...
1
vote
2answers
96 views

Bash: Echo a variable whose name is the value of another variable [duplicate]

Suppose i have the following: foo1=abc i=1 a="FOO${i}" echo ${${a}} echo ${`echo $a`} # I also tried that I am getting the error bash: ${${a}}: bad substitution. Thanks
1
vote
1answer
46 views

Any way to create Here Documents that substitute some variables but not others in bash?

I was wondering if there any way to create a here document in bash to output to a file within a script, problem is that in this config file I am trying to output some variables that start with $ I ...
5
votes
2answers
579 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 ...
16
votes
2answers
1k 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'...
1
vote
1answer
81 views

bash adds apostrophes to expanded variable

I am reading in a file inside my bash script where each line consists of multiple columns like this: ANSIBLE 'first run' '--diff --extra-vars "client=yes fast=no"' As you can see, the line consists ...
3
votes
3answers
46 views

Assigning upper case variables to commands

While trying to decipher some scripts written by former employees of my current company, in many of the scripts, I came across the following statements which assign variables to some commands as shown ...
1
vote
1answer
48 views

Bash: Default variable with command substitution

Is there any way to accomplish the following in one stroke using Bash? my_var=${$(grep -E '^setting_1' /settings.conf):-default_setting1} As opposed to: my_var=$(grep -E '^setting_1' /settings.conf)...
4
votes
1answer
149 views

Append (alter) each array element via parameter expansion (i.e. without printf)?

Let the script below exemplify my quandary.. #!/bin/zsh STUFF=( moose-hoof ovary clydsedale ) echo ${MINE=$(printf "MY-%s " $STUFF)} echo ${MINE_EXP=${STUFF/^/MY-}} MY-moose-hoof MY-ovary MY-...
6
votes
2answers
406 views

Why do here-documents attempt shell substitution even on a commented line?

I wanted to see how some ASCII art looked in terminal so: $ cat <<EOF ...
1
vote
4answers
38 views

Testing for any of a family of related values in a Bash variable [closed]

Apologies if this has been answered previously, but I couldn't find much online. Here's what I'm trying to accomplish: if [[ "${hostname}" = (foo | bar)servername(01 | 02 | 03) ]]; then var="...
2
votes
2answers
37 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 ...
0
votes
1answer
71 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
275 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
2answers
44 views

Find the position of repeated character “&”

How can determine the position of “&” in the column number 5 to substring the value 2 or substring value 3 by awk functions column1|column2|column3|column4|variable1:value1 & variable2:...
2
votes
2answers
136 views

while-loop doesn't seem to remember variable update - no piping involved

I am running LMDE 2 ( 'Betsy') with the standard Mate desktop and I have the following script: #!/bin/bash # This script starts a specified terminal-binary in "Always on Top"-mode # The assumption is,...
0
votes
1answer
119 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 ...
0
votes
1answer
128 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
3answers
344 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 $ ...
0
votes
1answer
203 views

Jquery like templates for formatting strings in bash

#!/bin/bash rm all for f in assets/css/*.css; do printf "<style type='text/css' >\n" >> all cat $f >> all printf "</style>\n <!-----$f---->" >> all echo "$f ...
0
votes
1answer
143 views

special symbols, . * # inside curly brackets [duplicate]

bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.} echo "BASH VERSION --- $BASH_VERSION" echo "bmajor ----- $bmajor" echo "bminor ----- $bminor" prints, BASH VERSION --- 4.2.46(1)-...
1
vote
2answers
86 views

Substrings in shell script

Am trying to get a substring from a string but am getting the error: ${curr_rec:3:4}: bad substitution #!/bin/ksh get_file_totals() { if [ -e "$file_name" ] then IFS='' ...
1
vote
2answers
105 views

bash + how to define array variable with instance number

Is it possible to define variable that is called for example machine1 as machine$counter ( while counter=1 ) ? For example, I created the /tmp/config.txt file and set the machine1 as array: $ more /...
2
votes
1answer
280 views

Passing variables to range patterns in awk

I'm trying to use range patterns in awk: grep -a volume somefile | awk '/^Apr 25 23:44:04*/,/^Apr 26 12:44:01*/ {print}' This works fine, but when I try to execute it by putting the date and time ...
2
votes
1answer
49 views

Expand variable in brace expansion [duplicate]

I am trying to enumerate a range of integers using a variable but having some trouble. When I type echo {1..5} I get 1 2 3 4 5 However, when I type a variable, I am unable to get the ...
0
votes
1answer
31 views

need help with inserting variables in a file path on GNU linux (SLES 11)

I am tryin to insert 3 variables to build a path, but unable to get it . I am trying the below log_path="/vol02/logs/$dname/logs/103602_$msname/${msname}_start.log" dname=cfp msname=cfp003 i get ...
4
votes
1answer
71 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....
3
votes
3answers
4k views

Variable not expanding inside another variable bash

I am reading a csv file in bash script as follows: resource='/data/bscs/' while IFS='|'read seqid fname fpath do echo "FILENO: $seqid" echo "FILENAME: $fname" echo "...