Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I tried a lot of ways to pass values to some variable i made up and it works partially, i can successfully do
TIME=12:55
printf $TIME
and to get the output '12:55', but i can't pass an output of a command 'hwclock' to the variable.. i tried 'TIME=$hwclock', 'TIME=hwclock' 'hwclock > TIME'
'$hwclock > TIME' but those wont work as i need them to.
How can i accomplish this?
Last edited by Infernal211283; 01-11-2006 at 03:07 AM.
Command Substitution
Command substitution allows the output of a command to replace the command itself. Command substitution occurs when a command is enclosed as follows:
$(command)
or
`command`
Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting. The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).
When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by `$', ``', or `\'. The first backquote not preceded by a backslash terminates the command substitution. When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.
Command substitutions may be nested. To nest when using the backquoted form, escape the inner backquotes with backslashes.
If the substitution appears within double quotes, word splitting and filename expansion are not performed on the results.
When using the $(command) form, all characters between the parentheses make up the command; none are treated
specially.
executing 'TIME=$(hwclock)' worked partially, i mean, the output of the command is usually 'Wed 11 Jan 2006 11:17:44 AM IST -0.928094 seconds' but the value $TIME stored is only 'Wed' - the [space] between 'Wed' and '11' broke the string that was supposed to be in the variable which shouldn't happen according to the above, should it?
being slow i can now say that i figured out that the error is because the file cannot be created with ':', so i turned to alternative command 'date' using it's formats (%y.%m.%d), now it all works, sorry for odd questions.
Just to enrich your learning about shell.
In general, the long format for date isn't useful for scripts and so. May be you prefer a more compact form of time. Something like this:
miguel@gold:~> date +%Y%m%d-%H%M%S
20060111-092317
yep, just what i thought after i saw how files look if they're named after 'hwclock', i'm already trying to make a script using all this neat knowledge, hopefully i'll manage to create something intelligent.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.