Command expansion and variables


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Command expansion and variables
# 1  
Old 06-14-2013
Command expansion and variables

Command expansions are pretty useful in some situations, however, I am not understanding why expansions do not update files, folders, directories, or even the current time, when there are placed in variables. For example:

Code:
#create variable
d=$(date)


result

Code:
echo $d
Fri Jun 14 15:48:04 EDT 2013
echo $d

Fri Jun 14 15:48:04 EDT 2013
echo $d

Fri Jun 14 15:48:04 EDT 2013
echo $d

Fri Jun 14 15:48:04 EDT 2013
echo $d

I would assume that it would up the the current time.
# 2  
Old 06-14-2013
You only ran the date command once, and stored it in a (static) variable, which you then "echo'd" a few times.

If you want the date to be "up-to-date", you need to run date every time you need it.

Code:
[root@lfs1 ~]# d='$(date)'
[root@lfs1 ~]# eval echo $d
Fri Jun 14 23:58:20 CEST 2013
[root@lfs1 ~]# eval echo $d
Fri Jun 14 23:58:21 CEST 2013
[root@lfs1 ~]# eval echo $d
Fri Jun 14 23:58:23 CEST 2013

# 3  
Old 06-14-2013
How would I got about getting the variable to update the command?
# 4  
Old 06-14-2013
If all you're going to do is echo it, just run the date command. If you just want a shorthand method of printing the date, create an alias or a function and call them when you want to print the date.
# 5  
Old 06-14-2013
Quote:
Originally Posted by Scott
If all you're going to do is echo it, just run the date command. If you just want a shorthand method of printing the date, create an alias or a function and call them when you want to print the date.
I was just attempting to understand why it was acting this way.
# 6  
Old 06-14-2013
OK. Is it clear now?
# 7  
Old 06-14-2013
I am new to unix, so I kind of understand, but I just assumed that putting a simple command like date or ls, in a variable, it would output the same as the original command.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use parameter expansion over a parameter expansion in bash.

Hello All, Could you please do help me here as I would like to perform parameter expansion in shell over a parameter expansion. Let's say I have following variable. path="/var/talend/nat/cdc" Now to get only nat I could do following. path1="${path%/*}" path1="${path1##*/}" Here... (8 Replies)
Discussion started by: RavinderSingh13
8 Replies

2. UNIX for Dummies Questions & Answers

Using find command with variables

I have a number of files in the /tmp directory with PET-DOG in their name. I want to delete them, leaving only files with PET-CAT and PET-HORSE. I'd like to use the find command to locate those files (by using a variable) and then I'd like to delete them. However, I can't find a way to do this. I... (3 Replies)
Discussion started by: newbie2010
3 Replies

3. Shell Programming and Scripting

Need help with paste command using variables

How can I accomplish this? I basically want to merge two variables onto the same line. I can do it with two FILES this way: $ cat /tmp/users_in.list | awk -F "," '{print $2}' | cut -c -1 > first.initial $ awk -F "," '{print $1}' /tmp/users_in.list | awk '{print $1}' > last.name $ paste... (5 Replies)
Discussion started by: greenlightening
5 Replies

4. Shell Programming and Scripting

Variables in an awk command

I am scripting a process where usernames are added to the end of a specific line in a file. using this: awk 'NR==1{$NF=$NF" TEXT"}1' myfileworks, and then I would use > tempfile && mv tempfile myfile HOWEVER. Being that this is to be scripted, I am trying to do something like: awk:... (8 Replies)
Discussion started by: magicjoe
8 Replies

5. Shell Programming and Scripting

[Solved] Command Substitution and Variable Expansion within a Case

Hello All, I don't write scripts very often, and in this case I am stumped, although it may be a bug in the version of bash I have to use (it's not my system). I want to extract a specific string snippet from a block of text (coming from a log file) that is dependent on a bunch of other... (1 Reply)
Discussion started by: jaimielives
1 Replies

6. Shell Programming and Scripting

Variables in SED command

Hi all, I want write a script to display 5rows at times from a input file. my out like: echo " display started" r1 r2 r3 r4 r5 ... Some action items... again i need next 5 lines. can you please advise. (2 Replies)
Discussion started by: koti_rama
2 Replies

7. UNIX for Dummies Questions & Answers

sed insert command and variable expansion/command substitution

I know this script is crummy, but I was just messing around.. how do I get sed's insert command to allow variable expansion to show the filename? #!/bin/bash filename=`echo $0` /usr/bin/sed '/#include/ { i\ the filename is `$filename` }' $1 exit 0 (8 Replies)
Discussion started by: glev2005
8 Replies

8. UNIX for Dummies Questions & Answers

Get command with variables

I'm writing a script to get a datestamped file from a ftp server. It seems that the "get" command does not respect variables. Anyone knows how to fix the problem? I'm trying to get a file named "rcsnotesYYYYMMDD" Here's my script today=`date +%Y%m%d` tput clear user <login> <password>... (1 Reply)
Discussion started by: mliu3
1 Replies

9. Shell Programming and Scripting

Need to print the expansion of the found string (the expansion is beween two delimiters '-' , '||'

Hi , could anyone help me out with this problem. sample.txt has this content : u001- this is used for project1 || u002- this is used for p2|| not to be printed u003- this is used for project3 || u004- this is used for p4 || u005- this is used for project5 || u006- this is used for p6... (9 Replies)
Discussion started by: Balaji PK
9 Replies

10. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies
Login or Register to Ask a Question

Featured Tech Videos