Partial variable substitution in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Partial variable substitution in script
# 1  
Old 01-02-2015
Partial variable substitution in script

I have a script.

Code:
filecreatenew ()  {
        touch /usr/src/$1_newfile.txt
        var=$1
        echo $var
        touch /usr/src/$var_newfile_with_var.txt
}

filecreatenew myfile

Its creating file /usr/src/myfile_newfile.txt as the variable $1 is correctly used. When $ is assigned to $var, the variable in $var is echoed fine, but the file is not getting created. How can I get the file myfile_newfile_with_var.txt correctly with the above script. ie the variable passed from $1 to $var should work in the line "touch /usr/src/$var_newfile_with_var.txt"
# 2  
Old 01-02-2015
Try:
Code:
touch "/usr/src/${var}_newfile_with_var.txt"

Otherwise the script will be trying to expand the non-existing variable $var_newfile_with_var. An underscore can be used as part of a variable name.

--edit--
In the case of
Code:
touch /usr/src/$1_newfile.txt

The braces are not strictly required, because $1 will be interpreted as a positional parameter no matter what follows (also - and perhaps because of this - variable names may not begin with a number).

But it will not hurt to use braces for clarity:
Code:
touch "/usr/src/${1}_newfile.txt"


Last edited by Scrutinizer; 01-02-2015 at 06:02 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 01-02-2015
Scrtinizer,

It worked thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script having variable substitution problems

Hi I am setting the variables like this : setenv MODULE1 modem5__3 setenv MODULE2 modem5__2 setenv MODULE3 modem_ctrl_1_1 setenv MODULE4 modem_1_0 setenv COUNT 10 I am having a bash script as shown below ################################################ #!/bin/bash for ((... (5 Replies)
Discussion started by: kshitij
5 Replies

2. Shell Programming and Scripting

sed variable substitution in a script

Hi I am trying to do the following in a script find a string and add in a block of text two lines above on the command line this works fine #/usr/bin/cat /usr/local/etc/dhcpd.conf_subnet | /usr/xpg4/bin/sed -n -e '1h;1\!H;${;g;s/}.*#END of 10.42.33.0/#START of RANGE $dstart\:option... (3 Replies)
Discussion started by: eeisken
3 Replies

3. Shell Programming and Scripting

awk syntax(partial) in variable

if a variable has part of awk syntax stored in it. for eg: x=if($1>100) can we substitute this variable in an awk statement. based on above requirement can we execute something like: awk '{x print $1}' infile (5 Replies)
Discussion started by: 47shailesh
5 Replies

4. Shell Programming and Scripting

AWK - Print partial line/partial field

Hello, this is probably a simple request but I've been toying with it for a while. I have a large list of devices and commands that were run with a script, now I have lines such as: a-router-hostname-C#show ver I want to print everything up to (and excluding) the # and everything after it... (3 Replies)
Discussion started by: ippy98
3 Replies

5. Shell Programming and Scripting

How to use variable with command substitution in variable

For example I have variable like below echo $OUTPUT /some/path/`uname -n` when I try to use the variable OUTPUT like below cd $OUTPUT or cd ${OUTPUT} I am getting bad substituion error message $ cd $OUTPUT ksh: cd: bad substitution $ cd ${OUTPUT} ksh: cd: bad substitution ... (1 Reply)
Discussion started by: rajukv
1 Replies

6. Shell Programming and Scripting

Making script show command (e.g. copy) being executed and variable substitution?

When script is running you only see when some of the commands are not successfull. Is there a way to see which command are executed and to show the substitution of variables as every line is executed ? (3 Replies)
Discussion started by: gr0124
3 Replies

7. Shell Programming and Scripting

Variable Substitution

Hi , I have a variable as follows, Temp=`cat ABC.txt | cut -c5-` This will yeild a part of the date. say , 200912. I would like to substitute this variable's value in a filename. eg: File200912F.zip when i say File$TempF.zip , it is not substituting. Any help ? Thanks in... (2 Replies)
Discussion started by: mohanpadamata
2 Replies

8. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

9. UNIX for Dummies Questions & Answers

variable substitution

Hi everyone, I have a simple question to ask : In a script that I'm writting, I need to create variables on-the-fly. For instance, for every iterartion of the following loop a var_X variable should be generated : #!/bin/ksh a="1 2 3" for i in $a do var_${i}=$i echo "${var_$i}" done ... (1 Reply)
Discussion started by: ck-18
1 Replies

10. UNIX for Advanced & Expert Users

Substitution in a variable

Hey All, I'm trying to clean up a variable using sed but It dosn't seem to work. I'm trying to find all the spaces and replace them with "\ " (a slash and a space). For Example "Hello World" should become "Hello\ World". But it does nothing. If I put it directly into the command line it works... (3 Replies)
Discussion started by: spragueg
3 Replies
Login or Register to Ask a Question