Shell symbols in linux

Bash Scripting: Symbols

Symbol: #! /bin/bash

In this case, the file.txt is taken as the input, and the cat command then cats it out.

Symbol: >

This symbol, known as the file redirection operator, is typically used to redirect the contents of a command/file to another by overwriting it.

echo “hello world’ > file.txt

Here, the > symbol is similar to 1>. This is so because the 1 is a file descriptor for the standard output. Please note that the file descriptors are as follows:

0 — Standard input, stdin

1 — Standard output, stdout

2 — Standard error, stderr

In the previous scenario, the single forward arrow was equivalent to 1>. However, we can also write 2> to forward it to the standard error.

mcat file.txt 2 > file2.txt

Here, the 2> means that the error will be dumped into file2.txt.

Symbol: >>

The symbol >> is used to append and not to replace! The file redirection operator replaces or overwrites everything while the >> is used to append.

echo “this is the second line” >> file.txt

echo “this is the third line” >> file.txt

The latter will append the two lines to the file called file.txt. The result of file.txt will then be as follows:

Symbol: #

The hashtag is used to add one-line comments into scripts. These comments are not executed/run.

# this will dump the line into the file

echo “this is a file ” > file.txt

Unlike the #, which is a one-liner, the multi-line comments look more like this;

This is the comments section

This is the first line

This is the second line

echo “hello world”

Symbol: $#

The symbol $# is used to retrieve the length or the number of arguments passed via the command line. When the symbol [email protected] or simply $1, $2, etc., is used, we ask for command-line input and store their values in a variable. The symbol $# is used to retrieve the total number of arguments passed.

The latter should chuck out a value of 2 because there are 3 elements (hello, world, and again).

Symbol: &>

This symbol redirects both the standard output and the standard error.

In this case, the &> symbol redirects both the standard output and the standard error to the file called file.txt. Thus, both the output generated and the error generated is placed in the same file.

Symbol: \

You need to compare the string length or character lengths; this can be done via the symbols \ . These two symbols are used to compare character lengths.

echo “a is shorter than b”

echo “a is longer than b”

In this case, the word stored in a – or cat – has a character length of 3, while the word stored in b – or lynx -has a character length of 4. Thus the answer should be that “a is shorter than b.”

Symbol: ^^, ^ and ,,

Some symbols function to change the case of the characters.

^^ — to turn all characters to uppercase

^ — to turn the first letter to uppercase

,, — to turn all characters to all lowercase

Symbol: [email protected] or $*

The symbol [email protected] is equivalent to $* which is equivalent to $1 $2 $3 $4…

# the latter is equivalent to echo [email protected]

In this example, the $1, $2, $3, $4, and $5 are inputs from the command line. Alternatively, we could have written the following:

Symbol: $?

This particular symbol – $? – is used to get the exit status of the command previously passed.

echo “hello world” > file.txt

An exit status of 0 indicates that the process was completed successfully.

Читайте также:  Setup linux from iso

Symbol: $$

The symbol $$ stores the PID of the current shell.

In my case, it printed out the value 2443. This is the PID of the shell.

Symbol: 2>&1

The symbol 2>&1 redirects both the standard output and the standard error to the standard output.

ls 2 >& 1 > file.txt

In this case, all the standard output and if any error is generated, the standard error is both directed into the file called file.txt.

Bash scripting is a key scripting language that can be used to automate tasks. During bash scripting, we encounter much code, but we also encounter special characters or symbols that are unique to bash. These symbols each have a particular role in bash scripting, and they aren’t always obvious. In this tutorial, we reviewed a few key symbols used while writing bash scripts. Obviously, there are many symbols out there; however, some are encountered so frequently that it might be necessary to know them while bash scripting. So go forth, fearless of the symbol from here onwards!

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.

Источник

How to Use Bash Shell metacharacters in Linux

Using the Shell Metacharacters

Bash shell metacharacters are specific characters, generally, symbols, that have special meaning to the shell. Three types of metacharacters are pathname metacharacters, file name substitution metacharacters, and redirection metacharacters.

Using the Path Name Metacharacters

Some of the shell metacharacters have specific path name functions. These metacharacters simplify location changes within the directory hierarchy. Some examples of path name metacharacters are:

Tilde (

) character represents the home directory of the current user. It is a substitution that equates to the absolute path name of the user. To change directories to dir1 by using the

character, perform the following command:

) character is available in all shells except the Bourne shell.

Tilde (

) Character With a User Name

) character followed by a user name represents the home directory of the specified user. To change directories to the user2 home directory, perform the following command:

Dash (-) Character

The dash (-) character in the shell represents the previous working directory. You can use the dash character to switch between two specific directories. The shell automatically displays the current directory path.

To switch between the user1 and tmp directories, perform the following command:

Using the File Name Substitution Metacharacters

You can substitute some shell metacharacters for other characters. These metacharacters simplify commands. Some examples of file name substitution metacharacters are:

Asterisk (*) Character

The asterisk (*) character is also called the wild card character and represents zero or more characters, except the leading period (.) of a hidden file. To list all files and directories that start with the letter f followed by zero or more other characters, perform the following commands:

To list all files and directories that start with the letter d followed by zero or more other characters, perform the following command:

To list all files and directories that end with the number 3, preceded by zero or more characters, perform the following command:

Question Mark (?) Character

The question mark (?) character represents any single character except the leading period (.) of a hidden file. The question mark character is also called a wild card character.

To list all files and directories that start with the string dir and followed by one other character, perform the following command:

If no files match an entry with the question mark, an error message appears.

Square Bracket ([]) Characters

The square bracket ([]) characters represent a set or range of characters for a single character position. A set of characters is any number of specific characters; for example, [acb]. The characters in a set do not generally need to be in any order. For example, [abc] is the same as [cab].

A range of characters is a series of ordered characters. A range lists the first character, a hyphen (-), and the last character, for example, [a–z] or [0–9]. When you specify a range, arrange the characters in the order that you want them to appear in the output. Use [A–Z] or [a–z] to search for any uppercase or lowercase alphabetical character, respectively.

To list all files and directories that start with the letters a through f, perform the following command:

To list all files and directories that start with the letters f or p, perform the following command:

Читайте также:  Kali linux whonix настройка

Источник

Which Linux Characters Do Beginners Use Most?

An ad-free and cookie-free website.

Memorize a few symbols then know where to look them up quickly

Videos can also be accessed from our Full Stack Playlist 2 on YouTube.

Which Linux characters do beginners use most? | Linux Tutorial for Beginners (4:31)

Welcome. Today’s question: Which Linux characters do beginners use most?

I’m Paul, and believe me, I know that interpreting symbols in Linux is a challenge for those coming from Mac and Windows.

Tackling them all is unrealistic and our goal really is to navigate Linux enough to install a software stack for Data Science.

Step 1 — Context Defines What Symbols Do

So in this tutorial, I’ll cover must-know characters and keystrokes and prepare you to look the others up.

In any language, words have meaning depending on context, right? Well, characters in Linux, and the bash shell in particular, are the same. And because there are too many to memorize, I’ll point you to two sources for help, an online Bash Manual, and a Cheat Sheet summarizing non-alphanumeric characters.

Then we’ll practice with these nine essentials.

(And practice with a few other commands).

In the next video, we’ll cover the basics of configuration files.

Step 2 — GNU Bash Manual

I’ll hop over to a local Linux test server running Debian, and summarize what we’ll cover here using whatis . Then I’ll change directories to /notes , list files, and we’ll open one in a minute.

$ whatis sleep whatis less pwd ls wc exit sleep (1) — delay for a specified amount of time whatis (1) — display one-line manual page descriptions less (1) — opposite of more pwd (1) — print name of current/working directory ls (1) — list directory contents wc (1) — print newline, word, and byte counts for each file exit: nothing appropriate paul@fullstack:

$ pwd /home/paul paul@fullstack:

$ cd notes paul@fullstack:

/notes$ ls video0002.txt video0004.txt video0006.txt video0008.txt video0011.txt video0016.txt video0003.txt video0005.txt video0007.txt video0009.txt video0012.txt video0017.txt paul@fullstack:

Try an exercise, look at your keyboard and count the number of non-alphanumeric keys. So no letters, numbers or function keys.

The letter c, for example, can only mean the letter c; however, characters have many meanings, depending on context.

On my keyboard there are 32, and I went left to right and created a table shown here in less .

/notes$ less video0016.txt

tilde directory 18 \ backslash 3 ! exclamation, bang 19 / forward slash directory 4 @ ampersand, at 20 < open curly brace 5 # number, pound, hash comment 21 >close curly brace 6 $ dollar symbol 22 [ open bracket 7 % percent 23 ] close bracket 8 ^ caret 24 » double quote 9 & ampersand 25 ‘ single quote, apos. 10 * asterisk, star 26 : colon 11 ( open parenthesis 27 ; semicolon commands 12 ) close parenthesis 28 ? question mark 13 _ underscore 29 greater than redirection 15 + plus 31 . period, dot directory 16 = equal 32 , comma video0016.txt (END)

I put common names here and the context for those I’ll discuss here.

So the tilde symbol

comes up when we’re talking about directories, pound # for comments, and for dash — it’s options.

Why is it that characters have multiple meanings? First, it saves keystrokes and second, it simplifies complex instructions.

Step 2 — Find Help on Characters

You know how to access the man bash page, all 5,000 lines of it.

For those really hungry for bash knowledge, I suggest the free 175-page GNU Bash Manual, offered in a variety of formats.

Step 3 — Linux Cheat Sheet

I’ve summarized some of it in a Linux Cheat Sheet you can check out later. I’ll keep improving it, so check in periodically.

Step 4 — Practice with Non-Alphanumeric Characters

The dash character in Linux

On the most used symbols for beginners. First, let’s discuss dash — and view the man page for whatis .

/notes$ man whatis

/notes$ man whatis WHATIS(1) Manual pager utils WHATIS(1) NAME whatis — display one-line manual page descriptions SYNOPSIS whatis [-dlv?V] [-r|-w] [-s list] [-m system[. ]] [-M path] [-L locale] [-C file] name . DESCRIPTION Each manual page has a short description available within it. whatis searches the manual page names and displays the manual page descriptions of any name matched. name may contain wildcards (-w) or be a regular expression (-r). Using these options, it may be necessary to quote the name or escape (\) the special characters to stop the shell from interpreting them. index databases are used during the search, and are updated by the mandb program. Depending on your installation, this may be run by a periodic cron job, or may need to be run manually after new manual pages have been installed. To produce an old style text whatis database from the relative index database, issue the command: whatis -M manpath -w ‘*’ | sort > manpath/whatis where manpath is a manual page hierarchy such as /usr/man. OPTIONS -d, —debug (102 lines trimmed) AUTHOR Wilf. Fabrizio Polacco. Colin Watson. 2.7.0.2 2014-09-28 WHATIS(1)

Читайте также:  Драйвера на принтер canon inkjet mp140 series

But I could have used -h for help or -V for version.

/notes$ whatis -h Usage: whatis [OPTION. ] KEYWORD. -d, —debug emit debugging messages -v, —verbose print verbose warning messages -r, —regex interpret each keyword as a regex -w, —wildcard the keyword(s) contain wildcards -l, —long do not trim output to terminal width -C, —config-file=FILE use this user configuration file -L, —locale=LOCALE define the locale for this search -m, —systems=SYSTEM use manual pages from other systems -M, —manpath=PATH set search path for manual pages to PATH -s, —sections=LIST, —section=LIST search only these sections (colon-separated) -?, —help give this help list —usage give a short usage message -V, —version print program version Mandatory or optional arguments to long options are also mandatory or optional for any corresponding short options. paul@fullstack:

/notes$ whatis -V whatis 2.7.0.2 paul@fullstack:

Of course, dash is used in other contexts as well.

The slash character in Linux

Second, slash / , so this is very basic, but slashes denote folders. The first one being the root, or the base of the directory structure.

/notes$ pwd /home/paul/notes paul@fullstack:

The tilde character in Linux

, here (pointing to output above) refers to my home directory. So this (pointing to /home/paul/notes and this

/notes are the same place).

You can always do this to go home.

The semicolon character in Linux

Next, the semicolon ; character is for entering multiple commands at once.

$ cd notes; pwd; ls /home/paul/notes video0002.txt video0004.txt video0006.txt video0008.txt video0011.txt video0016.txt video0003.txt video0005.txt video0007.txt video0009.txt video0012.txt video0017.txt paul@fullstack:

The pound character in Linux

The pound # is a comment, meaning the rest of the line is ignored.

/notes$ #ls paul@fullstack:

The dot character in Linux

The dot . occurs in many contexts, but the most basic is that dot refers to the present directory. Using an ls -a shows dots here, one dot is the current directory and two dots points to one above.

/notes$ ls -a . video0002.txt video0004.txt video0006.txt video0008.txt video0011.txt video0016.txt .. video0003.txt video0005.txt video0007.txt video0009.txt video0012.txt video0017.txt paul@fullstack:

/notes$ ls .; ls .. video0002.txt video0004.txt video0006.txt video0008.txt video0011.txt video0016.txt video0003.txt video0005.txt video0007.txt video0009.txt video0012.txt video0017.txt notes

(So listing files with one dot printed the first two lines and with two dots it printed the third).

The greater than character in Linux

The greater than symbol > is used to create a file on the fly.

For example, I could do an ls on this directory and put it one directory above, calling it lstest.txt .

/notes$ ls . > ../lstest.txt paul@fullstack:

Then view it using less (followed by q to quit).

(So listing files with one dot printed the first two lines and with two dots it printed the third).

The pipe character in Linux

A similar symbol is pipe | , where output from one command is handed off to another.

Here we use wc to count files.

/notes$ ls | wc -l 12

The Ctrl+c keystroke combination in Linux

And last is the keystroke combination Ctrl+c . If you ever get to the point where you’re lost and don’t see a command prompt, it means a program is running.

I’ve used one called sleep to mimic what you might see.

/notes$ sleep 1m ^C paul@fullstack:

Here Ctrl+c will stop that process.

Step 5 — Next: Configuration Files

We’re just scratching the surface on characters here, and trying to learn enough Linux to get our software stack installed so we can start playing around with Math and Statistics in Python.

  • Client : HTML, CSS, JavaScript
  • Software : Python Scientific Stack
  • Data : PostgreSQL, MySQL
  • OS : Linux (command line), Debian

In our next video we’ll talk about configuration files.

Have a nice day.

What’s Next?

We have hundreds of free videos on YouTube. Subscribe and check it out.

  • To access all tutorials, click Outline.
  • To learn about bash shell builtins, click Back.
  • It’s simple economics, operating an ad-free website requires support. Push Tip.
  • To learn about Linux configuration files, click Next.

Источник

Поделиться с друзьями
КомпСовет
Adblock
detector