need help understanding mv


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help understanding mv
# 8  
Old 09-28-2009
At a basic level the curly braces enclose the name of the variable such that it stands alone as an entity.

If I want to substitute the contents of the string into a variable or a command we can get problems:

file1="myfilename"
echo "$file1MYSTRING"

sh: file1MYSTRING: Parameter not set

This is because the shell doesn' know when the parameter name ends and the string starts.

file1="myfilename"
echo "${file1}MYSTRING"

myfilenameMYSTRING

Similarly distinguishing variables $file1 from $file11 which cropped up on this board recently.

Get it?
# 9  
Old 09-28-2009
Quote:
Originally Posted by taiL
Thanks guys. Can you briefly explain the curly brackets.

In the example shown, they are unnecessary.

They are only needed when the variable name is followed by a character that could be part of a variable name.
# 10  
Old 09-28-2009
To cfajohnson. I did not expect this response from you who I recognise as an expert in shell syntax. The O/P script did not contain the problem, my initial response did not contain the problem, but the example was designed to teach.

Please explain the error message in my example in your own words.
# 11  
Old 09-28-2009
Quote:
Originally Posted by methyl
To cfajohnson. I did not expect this response from you who I recognise as an expert in shell syntax. The O/P script did not contain the problem, my initial response did not contain the problem, but the example was designed to teach.

Please explain the error message in my example in your own words.

I was not commenting on any error message. I was answering the question, "Can you briefly explain the curly brackets."

My comment was about this:

Code:
mv "${file1}" "${file1}.TMP"
mv "${file2}" "${file2}.TMP"

The braces are not necessary in that example. They are not wrong, but they do nothing.

If the example had been:

Code:
mv "${file1}" "${file1}_TMP"
mv "${file2}" "${file2}_TMP"

...then they would have been necessary but only in the second instance on each line.
# 12  
Old 09-28-2009
Thanks cfajohnson. No offence taken or meant, but pedandtic or what? One day the O/P will appreciate this input and we may have to do a contract on that site.
# 13  
Old 09-30-2009
lol thanks guys. I greatly appreciate the clarification.
# 14  
Old 10-01-2009
Quote:
Originally Posted by taiL
Thanks guys. Can you briefly explain the curly brackets.
Curly brackets separate the variable name from other text surrounding it. Eg.
Code:
echo ${foo}bar

means fetch variable foo's value and append it with text bar. Without curly brackets the shell would be searching for a variable called foobar. One could also write
Code:
echo $foo bar

, but that would produce a space between.

Regards,

pen
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need your help in understanding this

Hi, I found this in a script and I would like to know how this works Code is here: # var1=PART1_PART2 # var2=${var1##*_} # echo $var2 PART2 I'm wondering how ##* makes the Shell to understand to pick up the last value from the given. (2 Replies)
Discussion started by: sathyaonnuix
2 Replies

2. UNIX for Dummies Questions & Answers

Understanding lseek

I tried to use lseek system call to determine the number of bytes in a file. To do so, I used open system call with O_APPEND flag to open a file. As lseek returns the current offset so I called lseek for opened file with offset as zero and whence as SEEK_CUR. So I guess it must return the number of... (3 Replies)
Discussion started by: Deepak Raj
3 Replies

3. Shell Programming and Scripting

Help with understanding of alias

Hi, I saw the following explanation about alias in bash from gnu website, but I didn't get the meaning: Bash always reads at least one complete line of input before executing any of the commands on that line. Aliases are expanded when a command is read, not when it is executed. Therefore, an... (3 Replies)
Discussion started by: Roy987
3 Replies

4. Shell Programming and Scripting

Help understanding the script

Hi Guys, I am new to scripting , I am trying to rebuild a script based on an old script. Can someone help me figure out what the script is doing? This is only a part of the script. I am looking to interpret these two points in the scripts:- 1) test=`echo $?` while I do not... (3 Replies)
Discussion started by: rajsan
3 Replies

5. Shell Programming and Scripting

Understanding a regex

Hi, Please help me to understand the bold segments in the below regex. Both are of same type whose meaning I am looking for. find . \( -iregex './\{6,10\}./src' \) -type d -maxdepth 2 Output: ./20111210.0/src In continuation to above: sed -e 's|./\(*.\{1,3\}\).*|\1|g' Output: ... (4 Replies)
Discussion started by: vibhor_agarwali
4 Replies

6. Shell Programming and Scripting

Understanding <<EOF

Hi all I stuck with a problem. I want to understand the execution of the below code. Can any one please help me `sqlplus username/passwd@DB << EOF set serveroutput on declare begin sql_query; end; / commit / quit EOF` My ques is why do we use EOF and how does it help. (4 Replies)
Discussion started by: parthmittal2007
4 Replies

7. Solaris

Understanding 'du' command

Hi I have a questions related 2 commands : 'du' and 'ls'. Why is the difference between output of 'du' and 'ls' cmd's ? Command 'du' : ------------------ jakubn@server1 /home/jakubn $ du -s * 4 engine.ksh 1331 scripts 'du -s *' ---> shows block count size on disk (512 Bytes... (5 Replies)
Discussion started by: presul
5 Replies

8. UNIX for Dummies Questions & Answers

understanding {%/*}/

Hi Gurus: I am trying to understand the following line of code.I did enough of googling to understand but no luck.Please help me understand the follow chunk of code: X=$0 MOD=${X%/*}/env.ksh X is the current script from which I am trying to execute. Say if X=test.ksh $MOD is echoing :... (3 Replies)
Discussion started by: vemana
3 Replies

9. Shell Programming and Scripting

Need help for understanding of script

# sub: find block (in cols), return line-numbers (begin-end) or 0 if notfound sub findb{ my ($exp1,$col1,$exp2,$col2)= @_; # $exp = expression to find, $col - column to search in my $cnt=0; my ($val1,$val2); my ($beg,$end); for($cnt=1;$cnt<=65536;$cnt++){ $val1 =... (3 Replies)
Discussion started by: suvenduperl
3 Replies
Login or Register to Ask a Question