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.
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.
Can I execute a shell command and put the result in command field?
Sometimes I have long directory path to type in command field of linux shell, it is very easily mistyped.
Such as :
can I execute "pwd" in command line and make the "pwd" result shown in the command line area and let me modify the command line area to execute another command.
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,
Code:
echo `pwd`
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;
Code:
sample .bash_profile;
~$ cat .bash_profile
# .bash_profile
#08-30-06 12:21
#
# Source .bashrc
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
Code:
sample .bashrc;
:~$ cat .bashrc
#.bashrc
#08-30-06 12:20
# Add bin to path
export PATH="$PATH:/sbin:/usr/sbin:$HOME/bin"
#export PATH="$PATH:$HOME/bin"
# Dynamic resizing
shopt -s checkwinsize
# Custom prompt
#PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
#08-29-06 11:40
if [ `id -un` = root ]; then
PS1='\[\033[1;31m\]\h:\w\$\[\033[0m\] '
else
PS1='\[\033[1;32m\]\h:\w\$\[\033[0m\] '
fi
#
# Add color
eval `dircolors -b`
# User defined aliases
alias cls='clear'
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 na='nano'
alias web='links -g -download-dir ~/ www.google.com'
#08-29-06 11:50
#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';
Quote:
excerpt 'man history';
NAME
history - GNU History Library
COPYRIGHT
The GNU History Library is Copyright (C) 1989-2002 by the Free Software Foundation,
Inc.
DESCRIPTION
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.
HISTORY EXPANSION
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-
able.
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.
Event Designators
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'.
!string
Refer to the most recent command starting with string.
!?string[?]
Refer to the most recent command containing string. The trailing ? may be
omitted if string is followed immediately by a newline.
^string1^string2^
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.