The tag has no usage guidance.

learn more… | top users | synonyms

14
votes
1answer
964 views

Why does variable expansion without $ work in expressions?

#!/bin/bash VALUE=10 if [[ VALUE -eq 10 ]] then echo "Yes" fi To my surprise, this outputs "Yes". I would have expected it to require [[ $VALUE -eq 10 ]]. I've scanned the CONDITIONAL ...
8
votes
1answer
2k views

Variable substitution with an exclamation mark in bash

I have the following lines in my .cfg bash script file DDF_SOURCE="siebel_DATA_DATE_FORMAT" DATA_DATE_FORMAT=${!DDF_SOURCE} how is ${!DDF_SOURCE} evaluated? It would be !siebel_DATA_DATE_FORMAT, ...
8
votes
2answers
2k views

Dereference concatenated variable name

I can do this, but it requires making a string of the variable then dereferencing it. Is there any way to shorten it to a simpler statement? #!/bin/bash FRUITS="BANANA APPLE ORANGE" ...
8
votes
1answer
10k views

Executing commands with ssh and shell script using variables on a remote machine

I'd like to execute a command and script located on a remote machine with a script on a local machine. I know it's possible to execute these kind of commands with ssh, so I made: #!/bin/bash ssh ...
7
votes
2answers
10k views

Why is my variable local one 'while read' loop, but not in another seemingly similar loop? [duplicate]

Why do I get different values for $x from the snippets below? #!/bin/bash x=1 echo fred>junk ; while read var ; do x=55 ; done <junk echo x=$x # x=55 .. I'd expect this result x=1 cat ...
7
votes
1answer
624 views

Why is bash extended-globbing variable substitution acting at the byte level?

I thought that bash variable substitution and globbing worked at character resolution, so I was rather surprised to see it acting at the byte level. Everything in my locale is en_AU.UTF-8 When ...
6
votes
2answers
793 views

Piping bash string manipulation

I've read some other piping bash string manipulation questions but they seem to be specialized applications. Essentially, is there a way to do the below simpler? instead of $ string='hello world'; ...
5
votes
4answers
15k views

Use a variable reference “inside” another variable

I'm sure it is relatively simple, I just don't know how to do it. #!/usr/bin/ksh set `iostat` myvar=6 I want to something like echo ${$myvar} which i want interpreted as ${$myvar} -> ${6} -> value ...
5
votes
3answers
2k views

How to generate new var names on the fly

I'm trying to generate dynamic var names in a shell script to process a set of files with distinct names in a loop as follows: SAMPLE1='1-first.with.custom.name' SAMPLE2='2-second.with.custom.name' ...
5
votes
5answers
5k views

Double and triple substitution in bash and zsh

Follow-up to the background part in this question. In bash I can use {!FOO} for double substitution, in zsh ${(P)FOO}. In both, the old-school (hack-y) eval \$$FOO works. So, the smartest and most ...
5
votes
3answers
2k views

How can I pass argument stored on a variable to WGET

I'm writing a bash script that extensively uses wget. To define all common parameters in one place I store them on variables. Here's a piece of code: useragent='--user-agent="Mozilla/5.0 (Windows NT ...
4
votes
3answers
474 views

${!FOO} and zsh

${!FOO} performs a double substitution in bash, meaning it takes the (string) value of FOO and uses it as a variable name. zsh doesn’t support this feature. Is there a way to make this work the same ...
4
votes
3answers
70 views

Can command substitution be nested in variable substitution?

I would like to use variable substitution on a particular string that I access via a command. For example, if I copy something into my clipboard, I can access it like this. $ xclip -o -selection ...
4
votes
1answer
67 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, ...
4
votes
2answers
4k views

bash ${VAR//search/replace} and weird regex behaviour

I am trying to do some searching and replacing on a variable using the ${VAR//search/replace} parameter expansion. I have a pretty long and evil PS1, that I want to work out the size of after ...
3
votes
3answers
810 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 ...
3
votes
2answers
864 views

pure shell complex substitution in variable

However it may be impossible, I hope I'm just bad man reader =) Is there any way to substitute text in variables on several patterns at time or even using back reference? For example, I have ...
3
votes
2answers
476 views

Bash parameter expansion - greedy versus non-greedy

It's a bit of a contrived example but here goes: Say I have a variable 1.2.3.4 containing version information and need to replace the .4 at the end with .5 version=1.2.3.4 echo ${version%.*}.5 ...
3
votes
2answers
11k views

Passing a variable to sed

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 ...
3
votes
1answer
118 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 ...
3
votes
1answer
804 views

variable expansion within command substitution over SSH bash 4.X

I want to do this ssh ${w100user}@web100 'ls -l "$(grep "${1}" /etc/pure-ftpd/pureftpd.passwd|cut -d':' -f6)"' Which obviously performs an ssh session to server web100 as w100user and then greps ...
3
votes
2answers
256 views

What is the effect of “${(@f)…}” in Zsh?

I ran into a Zsh script and wanted to know its meaning. In the scripts below there is ${$(@f)$(egrep "$2","$file")} expression. From what I searched, @ is to use represent all positional parameters, ...
3
votes
1answer
212 views

Service script calls another script with variables

I have a simple service script tomcat7 in /etc/init.d/ to start and stop my app server (tomcat) under the tomcat user. On RHEL6. #!/bin/bash # description: Tomcat Start Stop # processname: tomcat # ...
2
votes
3answers
547 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 ...
2
votes
2answers
353 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 ...
2
votes
1answer
346 views

shell variable in awk is not being passed to all the lines but just for the very first line of input?

inputfile:is2.txt 10.39.5.41,A1,B1 10.39.5.41,A2,B2 10.39.5.41,A3,B3 10.39.5.41,A4,B4 10.39.5.41,A5,B5 10.39.5.41,A6,B6 script : #!/bin/bash second_column="OOOOOOO" # OOOOOOO will be added to ...
2
votes
2answers
262 views

Picking a random file from each subfolder

Inspired by the accepted answer to this thread: List X random files from a directory, I tried to put together two lines that would pick one file from each subfolder of my current directory, with no ...
2
votes
3answers
2k views

Substitute placeholders in template

Say I have a shell configuration file config like this: HOST=localhost PORT=8080 Now I have a template template like this: The host is <%= @HOST %> The port is <%= @PORT %> How do I ...
2
votes
2answers
64 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
41 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 ...
2
votes
1answer
51 views

Running a constructed command from bash script

I constructed a command like this in a bash script a="my_cmd"; a="$a --verbose"; echo $a; $a; It works and executes my command correctly. But when I add an environment variable to the mix it ...
2
votes
2answers
279 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
2answers
161 views

assign output of a c program to a shell variable: token error

I have a shell script to run several times a .c program ('switch') which admit 3 input paramenters. I want to run the program 4 times passing values {4,16,32,64}, and for parameters ...
2
votes
1answer
88 views

Parameter substitution and error messages: suppressing line numbers etc

Here is my example file: !/bin/bash # argument-one # Is first argument missing? # First method [[ "$1" == "" ]] && echo "Usage: $(basename $0) filename"; exit 1 # Second method # ...
2
votes
1answer
3k 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?
2
votes
2answers
26 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 ...
2
votes
1answer
156 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 ...
1
vote
3answers
90 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 $ ...
1
vote
1answer
762 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 ...
1
vote
1answer
77 views

Replacing shell variable names in another variable

I've been wrestling with bash variable substitution for a while now and can't figure this out... I have a variable with a command template: CMD_TMPL='sudo -u ${USER_NAME} ${USER_HOME}/script.sh' ...
1
vote
1answer
221 views

Expand shell variable without globbing

I want to check whether an input string refers to a file name - not a wildcard string like *.txt. This doesn't work: if [ -f "$1" ]; Because $1 gets expanded to *.txt, which gets expanded to, say ...
1
vote
2answers
59 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
72 views

Why does zsh allow name to be null in ${name:-word}?

From zsh documentation: ${name-word} ${name:-word} If name is set, or in the second form is non-null, then substitute its value; otherwise substitute word. In the second form name may ...
1
vote
3answers
328 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 ...
1
vote
3answers
94 views

Variable scope in multiple pipes

I was trying to do something following, find . -name "*.dat" | get the basename of file | move filename returned by first command to basename returned by second command To give a concrete example, ...
1
vote
1answer
446 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 ...
1
vote
1answer
186 views

awk with variables in condition and in output redirection file

I would please like some help with this command because I didn't find anything in documentation that can cover everything I want. I have some variables that are global, so I would prefer to keep them ...
1
vote
1answer
178 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 ...
1
vote
1answer
287 views

How achieve variable indirection (refer to a variable whose name is stored in another variable) in tcsh

I'm trying to do the following: set a = kitten set temp_kitten = purr echo ${temp_$a} I want the echo command to return "purr". The overall idea is that I have a bunch of variables in an array and ...
1
vote
1answer
7k views

Adding a time stamp when moving a file in bash [duplicate]

#!/bin/bash while read server <&3; do #read server names into the while loop if [[ ! $server =~ [^[:space:]] ]] ; then #empty line exception continue fi echo "Connecting to - ...