In linux command cd, after make a dir, I can use "cd !!:1" to enter it, but what exactly does !!:1 mean, I can't search it by google, because it include special chars.
That will change directory to the first argument of the previous command
For example
% ls foo bar
% cd !!:1
is equivalent to
% ls foo bar
% cd foo
Also !!:0 gives you the actual command (less arguments), !!:2 the second argument, !!:$ the last argument, and !! the whole command line.
!! is short hand for the previous command. The :1 goes to the second parameter in the command, which in your previous command was the directory name. One of my favorite command-line shortcuts is sudo !!
Related
cd = 'cd !* ;set prompt="! $host `dirs` % "'
This alias is on a Red Hat Enterprise linux server I use commonly, and I can't figure out what it does for me. Any ideas?
I think it is meant to change the directory and display the previous directories in the prompt. !* is a bash history expansion meaning the previous command without the first word. E.g. if the previous command was cd project, then !* is project. See the History Expansion section in man bash, more specifically here.
dirs simply displays the stack of directories, and is a bash builtin, see the link for details.
However, in standard bash, you'd use export PS1=... instead of set prompt=, so perhaps the author of the alias meant it to be used in csh rather than bash, e.g. see here. The history expansion !* has the same semantics, e.g. see here for tcsh.
In csh aliasing, !* is the input the command received, e.g.
alias + emacs \!* & and running + somefile will open somefile using emacs and move it to the background.
Now to your line - it changes directory to whatever you give it (!*) and sets the prompt (the line written in your terminal before your writing zone) to the host name, path (as pwd) and %
Just a quick note - pay attention to the \ in my alias line - you must escape special characters in your alias and pay attention to this
In bash, to run the most recent command you used, you can either press the up arrow key on your keyboard and press Enter or type !! and press Enter. But, is there a command that you can use to run the command that was executed right before the most recent one? For example:
$ pwd
/home/john
$ ls
bin Documents Music Pictures Templates
Desktop Downloads output Public Videos
Typing in !! and pressing Enter will run the ls command again. But, is there something similar to the !! command that will let you run the pwd command in the same manner? If there is one, give some more details as to how it works.
You can use !n, where n is line number in your history. To get the second to last command you can then use !-2.
You can use !-2 like is mentioned here. Along with the up arrow, you can cycle back through your history with ctrl-p as well, forward with ctrl-n. You can also use ctrl-r and do reverse search for a command you've already entered.
Did you remember the beginning characters of last executed commands,
you can execute it by specify the beginning characters of the command with !
from your question, to execute ls again, use the !l,
and !p or !pw to execute pwd command.but it only execute the last executed one.
I am a beginner of bash. I encounter a problem like this:
$ "make -p"
when I type the above in bash command line, there is nothing to happen, no error, no result msg.
I have searched double quotes syntax of bash in many websites. All of these materials give similar interpretation as below:
https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html
and give examples like:
echo "argument"
I do not find something like "echo argument". Moreover, I find a strange difference between bash command line and bash scripts.
If I type a non-existing command in command line:
$ "holy shit"
$ "look that"
there is nothing to happen. But if I type it in bash scripts:
#!/bin/bash
"holy shit"
"look that"
and execute this script, an error msg will be throw out:
$ ./myshell
./myshell: line 2: holy shit: command not found
./myshell: line 3: look that: command not found
Would someone can help give a detailed interpretation about the effect of double quotes when they enclosed the whole command?
Why there is no output in command-line?
Why it is different between command line and scripts?
If you enter a command foo, the shell searches the directories listed in your PATH variable until it finds a command of this name. If there is none, you get the error message command not found.
If you enter a command, which contains at least one slash - for example ./foo or foo/bar -, the shell does not search the PATH, but assumes that you have already entered the correct path to your command. If it does not exist, you get the error message No such file or directory.
In your case,
"cd home"
searches for a file with name cd home somewhere along your PATH, but there is no file of this name, and you get command not found. If you enter
"cd /home"
the shell bypasses PATH-search and assumes, that there exists a directory named cd (i.e. the 3 letters c,d,space) in your current directory, and below it a file named home, with x-bit set. There is no such file (and no such directory) on your system, and you get the error message No such file or directory.
If you are in the mood of experimenting around, you could try the following:
mydir="cd "
mkdir "$mydir"
echo "echo Hello Stranger" >"$mydir/home"
chmod +x "$mydir/home"
"cd /home"
This should print Hello Stranger. Pay attention that in the assignment to mydir, there must be a single space between the cd and the closing quote.
The double quotes mean it is a string. You can do something like:
echo "Hello everybody"
either at the command line or the shell. Sometimes when people put stuff in quotes. you are supposed to replace what is in quotes with your own variable (removing the quotes), and sometimes people put quotes around the whole command you are supposed to type to show the what exactly you should type. For your example of "make -p" just type it without the quotes and it should work in both the command line and as a script.
I'm trying to understand what is the unix command !$.
For example, I know that the command !1 is used to run the history command number 1.
It seems like !$ runs the last command typed in the bash.
For example if I wrote mv folder12 folder123 and then I would write cd !$ I would actually preform cd folder123.
Is it correct that !$ runs the last command typed in the bash?
!$ matches the last argument of the previous command.
From man bash
yank-last-arg (M-., M-_)
Insert the last argument to the previous command (the last word of
the previous history entry). With an argument, behave exactly
like yank-nth-arg. Successive calls to yank-last-arg move back
through the history list, inserting the last argument of each line in
turn. The history expansion facilities are used to extract the last
argument, as if the "!$" history expansion had been specified.
Example
$ vi a
$ ls -l !$ # expands to "ls -l a"
-rw-rw-r-- 1 me me 30 18 abr. 22:00 a
See also What is your single most favorite command-line trick using Bash?:
I'm a fan of the !$, !^ and !* expandos, returning, from the most
recent submitted command line: the last item, first non-command item,
and all non-command items. To wit (Note that the shell prints out the
command first).
And also a good reading: The Definitive Guide to Bash Command Line History.
I need some help understanding following shell script line,
apphome = "`cd \`dirname $0\` && pwd && cd - >/dev/null`"
All I understand is, this is creating a variable called apphome.
This is not a valid shell code.
The shell don't allow spaces around =
For the rest, while this seems broken, it try to cd to the dir of the script itself, display the current dir & finally cd back to the latest cd place redirecting his standard output STDOUT to the /dev/null trash-bin (that's makes not any sense, cd display only on standard error STDERR when it fails, never on STDOUT)
If you want to do this in a proper a simple way :
apphome="$(dirname $0)"
That's all you need.
NOTE
The backquote
`
is used in the old-style command substitution, e.g.
foo=`command`
The
foo=$(command)
syntax is recommended instead. Backslash handling inside $() is less surprising, and $() is easier to nest. See http://mywiki.wooledge.org/BashFAQ/082
It seems to assign a command to the "apphome" variable. This command can be executed later.
dirname returns a directory portion of a file name. $0 is the name of the script this line contains (if I am not mistaken).
Now, executing dirname <name> will return a directory, and cd will use the value.
So, what it would do is execute three command in the row assuming that each one of them succeeds. The commands are:
cd `dirname [name of the script]`
pwd
cd -
First command will change directory to the directory containing your script; second will print current directory; third will take yo back to the original directory. Output of the third command will not be printed out.
In summary, it will print out a name of a directory containing the script that contains the line in question.
At least, this is how I understand it.