Can I execute a shell command and put the result in command field?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
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.
Using the output of a program directly might be handy in other tasks than changing working directory, so in that sense I'd still vote for that approach. In addition it should guarantee that you get what you want to, even if the unthinkable happened and somehow $PWD got overwritten just before you typed the command (not probable, but possible).
There are other ways of using the output, or rather "other notations," than the $(command) one. For example in Bash one can also use backticks,
which may or may not be faster to type, depending on your keyboard and fingers. Check the manual of your shell if you're using something different than Bash (if that does not work).
Along with other suggestion if you have the need to repeat entries then why not setup a .bashrc & .bash_profile for your user with aliases;
~$ cat .bash_profile
# Source .bashrc
if [ -f ~/.bashrc ]; then
:~$ cat .bashrc
# Add bin to path
# Dynamic resizing
shopt -s checkwinsize
# Custom prompt
if [ `id -un` = root ]; then
# Add color
eval `dircolors -b`
# User defined aliases
alias clls='clear; ls'
alias ll='ls -l'
alias lsa='ls -A'
alias lsg='ls | grep'
alias lsp='ls -1 /var/log/packages/ > package-list'
alias web='links -g -download-dir ~/ www.google.com'
#To clean up and cover your tracks once you log off
#Depending on your version of BASH, you might have to use
# the other form of this command
trap "rm -f ~$LOGNAME/.bash_history" 0
#The older KSH-style form
# trap 0 rm -f ~$LOGNAME/.bash_history
The .bashrc is very useful!
Do not forget about 'history';
excerpt 'man history';
history - GNU History Library
The GNU History Library is Copyright (C) 1989-2002 by the Free Software Foundation,
Many programs read input from the user a line at a time. The GNU History library
is able to keep track of those lines, associate arbitrary data with each line, and
utilize information from previous lines in composing new ones.
The history library supports a history expansion feature that is identical to the
history expansion in bash. This section describes what syntax features are avail-
History expansions introduce words from the history list into the input stream,
making it easy to repeat commands, insert the arguments to a previous command into
the current input line, or fix errors in previous commands quickly.
History expansion is usually performed immediately after a complete line is read.
It takes place in two parts. The first is to determine which line from the history
list to use during substitution. The second is to select portions of that line for
inclusion into the current one. The line selected from the history is the event,
and the portions of that line that are acted upon are words. Various modifiers are
available to manipulate the selected words. The line is broken into words in the
same fashion as bash does when reading input, so that several words that would oth-
erwise be separated are considered one word when surrounded by quotes (see the
description of history_tokenize() below). History expansions are introduced by the
appearance of the history expansion character, which is ! by default. Only back-
slash (\) and single quotes can quote the history expansion character.
An event designator is a reference to a command line entry in the history list.
! Start a history substitution, except when followed by a blank, newline, = or
!n Refer to command line n.
!-n Refer to the current command line minus n.
!! Refer to the previous command. This is a synonym for `!-1'.
Refer to the most recent command starting with string.
Refer to the most recent command containing string. The trailing ? may be
omitted if string is followed immediately by a newline.
Quick substitution. Repeat the last command, replacing string1 with
string2. Equivalent to ``!!:s/string1/string2/'' (see Modifiers below).
!# The entire command line typed so far.
Thanks to everyone. It really helps. But I am currently working as one man MIS, do'nt have enough time to test all these good suggestion shortly. All the suggestion will test and make the best use of them.