The tag has no wiki summary.

learn more… | top users | synonyms

13
votes
1answer
681 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 ...
7
votes
2answers
1k 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" ...
7
votes
1answer
495 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 ...
5
votes
4answers
4k 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
866 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' ...
4
votes
4answers
2k 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 ...
4
votes
1answer
1k 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, ...
4
votes
3answers
385 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
1answer
200 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 ...
3
votes
3answers
196 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 ...
3
votes
2answers
580 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
1answer
103 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
204 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
103 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
25 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
2answers
120 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 ...
2
votes
3answers
217 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
2k 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 ...
2
votes
1answer
281 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
167 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
1k 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
1answer
44 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
1k 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
1k 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 ...
2
votes
1answer
95 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
1answer
226 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
3answers
42 views

Pulling Lines X-Y From List of Files Found

Ultimately I need to grab lines 3-53 from ever csv in each subdirectory. I grabbed the lines like so (cat isn't necessarily required): cat /[path]/[file].csv | head -53 | tail -51 And the files ...
1
vote
1answer
28 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
36 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
3answers
73 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
341 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
2k 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 - ...
1
vote
1answer
1k views

Variable Substitution in Awk Print Statement -v

I've spent over an hour poking at this. It can't be this hard... I want to print a column of data from one file to another file. The column I want to print is dependent on what is passed in and stuff ...
1
vote
1answer
31 views

Where can I find documentation on shell script syntax for using a regex within a variable construct?

I'm debugging a shell script (which I didn't write) that contains this loop: read line < "$pid_file" for p in $line ; do [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p" done ...
1
vote
2answers
942 views

using bash script to output new script: need mixture of variables to be replaced and not replaced

I'm having a problem with bash variable substitution. Here's a silly example of what I am trying to do. I need to output a new script from a bash script. A line (see echo in my example) has a mixture ...
1
vote
3answers
387 views

Reevaluate the prompt expression each time a prompt is displayed in zsh

I'm adjusting my zsh prompt, based upon the dallas theme and the dstufft theme from oh-my-zsh. I love how dallas has various sections of the prompt contained in variables, which makes it much easier ...
0
votes
2answers
214 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
757 views

How to reassign new values to array elements?

I'm trying to figure out like three or four things at the same time.The most I need help with is how to get the greatest number in all the files that I have created on a prior run of my script, that ...
0
votes
1answer
341 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 ...
0
votes
1answer
142 views

Combining parameter expansion with modifiers in zsh

Say I have the following $f1=/some/path $f2=/some/subpath $f3=/some/other/subpath If I try: test1=${$f1/$f2/${f3:t}} zsh complains with bad substitution. However: test2=${f3:t} works well. ...
0
votes
4answers
497 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 ...
0
votes
1answer
40 views

Special Parameter query - multiple used to obtain command name? [duplicate]

In a uni text provided us to cover bash scripting, the following variable assignment has got be stumped and I've yet to get an answer back from anyone, hence hopefully someone on here can help. ...
0
votes
1answer
74 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 ...
0
votes
1answer
165 views

Why does a working standalone nested function/script not work inside a larger script? [duplicate]

The following (nested) function/s function hpf_matrix { # Positional Parameters Matrix_Dimension="${1}" Center_Cell_Value="${2}" # Define the cell value(s) function hpf_cell_value { ...
0
votes
1answer
98 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 ...
0
votes
1answer
87 views

Colon breaks the variable subsitution [closed]

I am trying to lookup some C functions so that I could debug while using strace. So I setup a bash function to look it up in firefox (or links), but the substitution falls apart with : and escaping ...
0
votes
1answer
577 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
1answer
69 views

Copy selected files had subtitutions in name

I have some folders: oldA with A_1.aaa, A_2.aaa, A_1.bbb, A.def, A.ghi oldB with B_1.aaa, B_2.aaa, B_1.bbb, B.def, B.ghi and so on and need to copy them to another folder with selection. I want ...
0
votes
0answers
26 views

Variable Substitution in sed [duplicate]

I have this script and I need to count the lines inside a file, then pass the number of lines counted to a for loop so that I can get the contents of each line and put it in a variable. My question ...
-2
votes
1answer
86 views

how do I make a $VAR empty so it don't effect the command line argument and still be within that argument?

This is the basic setting for hsetroot for it to work: hsetroot -center /path/to/image/ - setting the wallpaper with just that the picture. To use these options, just one has to be place within the ...