- 10 Amazing and Mysterious Uses of (!) Symbol or Operator in Linux Commands
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- What does * mean in Unix?
- What does * mean in shell script?
- What is an asterisk symbol in Linux?
- What does $? Mean in bash?
- What is the symbol in Unix?
- What is $0 shell?
- What is $1 in bash script?
- What does the * mean in Linux?
- What is this symbol called in Linux?
- How many types of wildcards are there?
- What is bash symbol?
- Why do we use Unix?
- What does P mean Linux?
- How do I pipe in Unix?
- What is symbol in Linux command?
- What is symbol called in Unix?
- What is $? In Linux?
- What does it mean
- What does >> mean in Unix?
- Is called in Linux?
- How many types of system commands are there?
- What is $1 in Linux?
- How do I know my current shell?
- What is $0 shell?
- What does to mean in text?
- What is the meaning of in path?
- What does
- Why do we use Unix?
- What is the output of who command?
- How do I run Unix?
10 Amazing and Mysterious Uses of (!) Symbol or Operator in Linux Commands
The ‘!’ symbol or operator in Linux can be used as Logical Negation operator as well as to fetch commands from history with tweaks or to run previously run command with modification. All the commands below have been checked explicitly in bash Shell. Though I have not checked but a major of these won’t run in other shell. Here we go into the amazing and mysterious uses of ‘!’ symbol or operator in Linux commands.
1. Run a command from history by command number.
You might not be aware of the fact that you can run a command from your history command (already/earlier executed commands). To get started first find the command number by running ‘history‘ command.
Now run a command from history just by the number at which it appears, in the output of history. Say run a command that appears at number 1551 in the output of ‘history‘ command.
And, it runs the command (top command in the above case), that was listed at number 1551. This way to retrieving already executed command is very helpful specially in case of those commands which are long. You just need to call it using ![Number at which it appears in the output of history command].
2. Run previously executed command as 2nd last command, 7th last command,etc.
You may run those commands which you have run previously by their running sequence being the last run command will be represented as -1, second last as -2, seventh last as -7,….
First run history command to get a list of last executed command. It is necessary to run history command, so that you can be sure that there is no command like rm command > file and others just to make sure you do not run any dangerous command accidentally. And then check Sixth last command, Eight last command and Tenth last command.
Run Last Executed Commands By Numbers
3. Pass arguments of last command that we run to the new command without retyping
I need to list the content of directory ‘/home/$USER/Binary/firefox‘ so I fired.
Then I realized that I should have fired ‘ls -l‘ to see which file is executable there? So should I type the whole command again! No I don’t need. I just need to carry the last argument to this new command as:
Here !$ will carry arguments passed in last command to this new command.
Pass Arguments of Last Executed Command to New
4. How to handle two or more arguments using (!)
Let’s say I created a text file 1.txt on the Desktop.
and then copy it to ‘/home/avi/Downloads‘ using complete path on either side with cp command.
Now we have passed two arguments with cp command. First is ‘/home/avi/Desktop/1.txt‘ and second is ‘/home/avi/Downloads‘, lets handle them differently, just execute echo [arguments] to print both arguments differently.
Note 1st argument can be printed as “!^” and rest of the arguments can be printed by executing “![Name_of_Command]:[Number_of_argument]” .
In the above example the first command was ‘cp‘ and 2nd argument was needed to print. Hence “!cp:2” , if any command say xyz is run with 5 arguments and you need to get 4th argument, you may use “!xyz:4” , and use it as you like. All the arguments can be accessed by “!*” .
Handle Two or More Arguments
5. Execute last command on the basis of keywords
We can execute the last executed command on the basis of keywords. We can understand it as follows:
Here we have used same command (ls) but with different switches and for different folders. Moreover we have sent to output of each command to ‘/dev/null‘ as we are not going to deal with the output of the command also the console remains clean.
Now Execute last run command on the basis of keywords.
Check the output and you will be astonished that you are running already executed commands just by ls keywords.
Run Commands Based on Keywords
6. The power of !! Operator
You can run/alter your last run command using (!!) . It will call the last run command with alter/tweak in the current command. Lets show you the scenario
Last day I run a one-liner script to get my private IP so I run,
Then suddenly I figured out that I need to redirect the output of the above script to a file ip.txt, so what should I do? Should I retype the whole command again and redirect the output to a file? Well an easy solution is to use UP navigation key and add ‘> ip.txt’ to redirect the output to a file as.
Thanks to the life Savior UP navigation key here. Now consider the below condition, the next time I run below one-liner script.
As soon as I run script, the bash prompt returned an error with the message “bash: ifconfig: command not found” , It was not difficult for me to guess I run this command as user where it should be run as root.
So what’s the solution? It is difficult to login to root and then type the whole command again! Also (UP Navigation Key) in last example didn’t came to rescue here. So? We need to call “!!” without quotes, which will call the last command for that user.
Here su is switch user which is root, -c is to run the specific command as the user and the most important part !! will be replaced by command and last run command will be substituted here. Yeah! You need to provide root password.
The Power of !! Key
I make use of !! mostly in following scenarios,
1. When I run apt-get command as normal user, I usually get an error saying you don’t have permission to execute.
Opps error…don’t worry execute below command to get it successful..
Same way I do for,
OOPS User not authorized to carry such task, so I run..
7. Run a command that affects all the file except ![FILE_NAME]
The ! (Logical NOT) can be used to run the command on all the files/extension except that is behind ‘!’ .
A. Remove all the files from a directory except the one the name of which is 2.txt.
B. Remove all the file type from the folder except the one the extension of which is ‘pdf‘.
8. Check if a directory (say /home/avi/Tecmint)exist or not? Printf if the said directory exist or not.
Here we will use ‘! -d’ to validate if the directory exist or not followed by Logical AND Operator (&&) to print that directory does not exist and Logical OR Operator (||) to print the directory is present.
Logic is, when the output of [ ! -d /home/avi/Tecmint ] is 0, it will execute what lies beyond Logical AND else it will go to Logical OR (||) and execute what lies beyond Logical OR.
9. Check if a directory exist or not? If not exit the command.
Similar to the above condition, but here if the desired directory doesn’t exist it will exit the command.
10. Create a directory (say test) in your home directory if it does not exist.
A general implementation in Scripting Language where if the desired directory does not exist, it will create one.
That’s all for now. If you know or come across any other use of ‘!’ which is worth knowing, you may like to provide us with your suggestion in the feedback. Keep connected!
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
What does * mean in Unix?
For example, the most commonly used special character is asterisk, * , meaning “zero or more characters”. When you type a command like ls a* , the shell finds all filenames in the current directory starting with a and passes them to the ls command. Quote marks affect the shell’s interpretation of the command line.
What does * mean in shell script?
It means all the arguments passed to the script or function, split by word.
What is an asterisk symbol in Linux?
There are three main wildcards in Linux: An asterisk (*) – matches one or more occurrences of any character, including no character. Question mark (?) – represents or matches a single occurrence of any character.
What does $? Mean in bash?
According to the bash manual , $? expands to the exit status of the most recently executed foreground pipeline. … In short, it’s the return value of the last executed command and if the command returned “0” (zero), then it succeeded: cd no_such_dir. if [ $? -eq 0 ]; then.
What is the symbol in Unix?
These are symbols that you can use at the Unix shell prompt as part of a command.
…
Symbol | Meaning |
---|---|
| | a Unix pipe |
> | redirect standard output |
> | redirect and append standard output |
What is $0 shell?
$0 Expands to the name of the shell or shell script. This is set at shell initialization. If Bash is invoked with a file of commands (see Section 3.8 [Shell Scripts], page 39), $0 is set to the name of that file.
What is $1 in bash script?
$1 is the first command-line argument passed to the shell script. Also, know as Positional parameters. … $0 is the name of the script itself (script.sh) $1 is the first argument (filename1) $2 is the second argument (dir1)
What does the * mean in Linux?
For example, the most commonly used special character is asterisk, * , meaning “zero or more characters”. When you type a command like ls a* , the shell finds all filenames in the current directory starting with a and passes them to the ls command.
What is this symbol called in Linux?
Basics of Linux Commands
Symbol | Explanation |
---|---|
| | This is called “Piping”, which is the process of redirecting the output of one command to the input of another command. Very useful and common in Linux/Unix-like systems. |
> | Take the output of a command and redirect it into a file (will overwrite the whole file). |
How many types of wildcards are there?
Wildcards in Excel are the special characters in excel which takes place of the characters in it, there are three wildcards in excel and they are asterisk, question mark, and tilde, asterisk is used to multiple numbers of characters in excel while question mark is used to represent only a single character whereas tilde …
What is bash symbol?
Special bash characters and their meaning
Special bash character | Meaning |
---|---|
# | # is used to comment a single line in bash script |
$$ | $$ is used to reference process id of any command or bash script |
$0 | $0 is used to get the name of the command in a bash script. |
$name | $name will print the value of variable “name” defined in the script. |
Why do we use Unix?
Unix is an operating system. It supports multitasking and multi-user functionality. Unix is most widely used in all forms of computing systems such as desktop, laptop, and servers. On Unix, there is a Graphical user interface similar to windows that support easy navigation and support environment.
What does P mean Linux?
With the help of mkdir -p command you can create sub-directories of a directory. It will create parent directory first, if it doesn’t exist. But if it already exists, then it will not print an error message and will move further to create sub-directories.
How do I pipe in Unix?
You can make it do so by using the pipe character ‘|’. Pipe is used to combine two or more commands, and in this, the output of one command acts as input to another command, and this command’s output may act as input to the next command and so on.
Источник
What is symbol in Linux command?
Symbol | Explanation |
---|---|
| | This is called “Piping”, which is the process of redirecting the output of one command to the input of another command. Very useful and common in Linux/Unix-like systems. |
> | Take the output of a command and redirect it into a file (will overwrite the whole file). |
What is symbol called in Unix?
So, in Unix, there is no special meaning. The asterisk is a “globbing” character in Unix shells and is wildcard for any number of characters (including zero). ? is another common globbing character, matching exactly one of any character. *.
What is $? In Linux?
$? -The exit status of the last command executed. $0 -The filename of the current script. $# -The number of arguments supplied to a script. … For shell scripts, this is the process ID under which they are executing.
What does it mean
/ is shorthand for the current user’s home folder. So if your user name is “foobar” it expands to /home/foobar/ Share.
What does >> mean in Unix?
redirects output to a file, overwriting the file. >> redirects output to a file appending the redirected output at the end.
Is called in Linux?
Basics of Linux Commands
Symbol | Explanation |
---|---|
| | This is called “Piping”, which is the process of redirecting the output of one command to the input of another command. Very useful and common in Linux/Unix-like systems. |
> | Take the output of a command and redirect it into a file (will overwrite the whole file). |
How many types of system commands are there?
The components of an entered command may be categorized into one of four types: command, option, option argument and command argument. The program or command to run. It is the first word in the overall command.
What is $1 in Linux?
$1 is the first command-line argument passed to the shell script. … $0 is the name of the script itself (script.sh) $1 is the first argument (filename1) $2 is the second argument (dir1)
How do I know my current shell?
How to check which shell am I using: Use the following Linux or Unix commands: ps -p $$ – Display your current shell name reliably. echo “$SHELL” – Print the shell for the current user but not necessarily the shell that is running at the movement.
What is $0 shell?
$0 Expands to the name of the shell or shell script. This is set at shell initialization. If Bash is invoked with a file of commands (see Section 3.8 [Shell Scripts], page 39), $0 is set to the name of that file.
What does to mean in text?
TO means “Time Out”
What is the meaning of in path?
It means to start from the current directory path . Let’s assume you have a path like this: /usr/ /usr/bin/ /usr/local/bin. and inside /usr/bin/ there is an executable called yiic . If you issue yiic it would start the one in /usr/bin/ .
What does
/. somefilename means your home directory, the file . somefilename .
Why do we use Unix?
Unix is an operating system. It supports multitasking and multi-user functionality. Unix is most widely used in all forms of computing systems such as desktop, laptop, and servers. On Unix, there is a Graphical user interface similar to windows that support easy navigation and support environment.
What is the output of who command?
Explanation: who command output the details of the users who are currently logged in to the system. The output includes username, terminal name (on which they are logged in), date and time of their login etc. 11.
How do I run Unix?
UNIX commands are strings of characters typed in at the keyboard. To run a command, you just type it in at the keyboard and press the ENTER key.
Источник