Click to See Complete Forum and Search --> : shell script question - Variables


pwharff
10-08-2003, 05:20 PM
I'm farely new to linux and I'm sure the answer to this question is very easy:

How do I make the output of a command like "pwd" become a variable in my script.

pwd = current_dir

Will obviously not work and will make the command "pwd" useless, as it will no longer execute "pwd" command but will use the new variable name. So how do I do this? I'm sure it's really trivial. BTW, this is my first post and hope to hang around here a lot in the future.

Hayl
10-08-2003, 05:39 PM
#!/bin/bash
foo="$(pwd)"
echo ${foo}

pwharff
10-08-2003, 05:48 PM
First off thanks! Question about the code, I'm guessing that the () around the pwd indicates that anything between this is a command? Is that correct? Also, I'm not sure why you included {} around foo? I tried it without, like :

echo $foo

And it works. So what are the {} useful for. Sorry, I just want to learn as I go. And thanks again for you help

Hayl
10-08-2003, 06:05 PM
Originally posted by pwharff
First off thanks! Question about the code, I'm guessing that the () around the pwd indicates that anything between this is a command? Is that correct? Also, I'm not sure why you included {} around foo? I tried it without, like :

echo $foo

And it works. So what are the {} useful for. Sorry, I just want to learn as I go. And thanks again for you help

() around a command allow you to assign the output to a variable.

${foo} is the same as $foo, just a little easier to read / better looking.

UID500
10-08-2003, 06:46 PM
there is a good starting out BASH tutorial on this site in the library.

bwkaz
10-08-2003, 06:51 PM
Originally posted by pwharff
First off thanks! Question about the code, I'm guessing that the () around the pwd indicates that anything between this is a command? Sort of. The dollar sign is also significant.

What $(any-command) does is, it executes any-command, then takes the output if it and replaces newlines with spaces (I think it also replaces tabs with spaces but I don't know for sure), and puts the output into its logical command holding area. So if the pwd command prints something like:

/home

, then foo=$(pwd) would turn into foo=/home inside the shell's command buffer.

Also, I'm not sure why you included {} around foo? It's a readability thing, partly, but it's also very useful if you wanted to do:

echo ${foo}bar

to tack "bar" onto the end of whatever was in the foo variable. Without the {}, the shell would see this:

echo $foobar

and it would try to echo the value of the foobar variable (which wouldn't be defined; you'd get an empty string echoed). But with the {}, it prints /homebar (or whatever).

pwharff
10-09-2003, 01:51 AM
Wow, thanks guys for all the help and explanation. The ${foo}bar example really helped and made a lot of sense. So far I have created about a dozen or so shell scripts using /bin/sh and know the basics as far as commands go and how the filesystem is arranged and why. I even know the basics for awk and sed. I just get a little hanged up every so often. I would usually post on another forum that wasn't specific to linux and that's why I am here, so I can further learn. Is there much difference in syntax when scripting with sh versus bash? Also where's that bash tutorial and do you think I need it? I read a learn UNIX in 24 hours book and it didn't go to much into detail as far as variables. Well anyway, this is a great start. Thanks again guys!!! :D

Stuka
10-09-2003, 11:25 AM
bash is essentially a superset of sh functionality. There may be areas of pure difference, but AFAIK, if it works in sh, it will work in bash.

dubbac
10-10-2003, 10:00 AM
Why wouldn't/shouldn't one use the following to assign the value of the pwd command to a variable:

foo=`pwd`
(those are back ticks for anyone new to bash, just in case they think they are single quotes...)

That should evaluate the pwd command and assign its value to foo and then referencable by $foo.

We use it all the time at work for renamine logs/etc with `date+%Y-%m-%d`.log etc....

just curious why one would choose one over the other.

bwkaz
10-10-2003, 07:17 PM
Because of what you said:

(those are back ticks for anyone new to bash, just in case they think they are single quotes...) It's harder to mistake the dollar sign and parentheses for anything else.

bash supports backticks, and they're exactly equivalent to $( and ), but backticks are easy to confuse with other types of quotes.

dubbac
10-13-2003, 10:06 AM
Thanks,

Guess I'll have to keep that in mind when i write my scripts. That way if I get hit by a bus and replaced by someone who doesn't see so well... =)

I actually hadn't heard of doing it the $() way before. I've only ever seen back ticks...

Thanks again