editing-files-in-linux-part1

Linux Essentials: Stdin, Stdout, Echo and Printf

Hello guys !! 👋 Today, we’ll explore some important concepts that every hacker should know, like how Linux handles input and output, along with essential commands like echo and printf. We’ll also cover some useful shortcuts in the terminal, including !!. Let’s jump right in!



What is Standard Input in Linux ? ( stdin )

Standard Input (often called stdin) is simply the way Linux accepts input. Whenever you type something in the terminal, Linux takes that input and passes it to the command you’re running.

  • Example: When you type a command like echo Hello, the Hello part is the input that the echo command processes and displays.

What is Standard Output in Linux ? ( stdout )

Standard Output (stdout) is how Linux shows you the result of a command. If you ask Linux to run a command, the answer (or output) is shown in the terminal.

  • Example: When you run echo Hello, the word “Hello” is printed on your screen as the output.

The echo Command in Linux

The echo command is one of the most commonly used Linux commands. It’s used to display text on the screen. But there are some tricks you can use with echo to format the text in different ways.

Single vs Double Quotes

  • Single Quotes ('): When you use single quotes, everything inside the quotes is shown exactly as it is. Escape sequences like \n (which is used to add a new line) don’t work here.
  • Double Quotes ("): When you use double quotes, Linux will treat special characters like \n and \t as commands to format the text.

Examples

Using echo with Single Quotes

Text is printed exactly as you type it there is no escape sequence characters or any extra features just simple text.

  echo 'Hello World !!'

Output:

  Hello World !!

Even if you use \n, it will still print as-is because single quotes don’t allow formatting:

  echo 'Hello\nWorld !!'

Output:

  Hello\nWorld !!

Using echo with Double Quotes

Here, Linux treats \n as a new line and additional special characters like !! can be used in the text and echo will automatically recognize them.

  echo "Hello\nWorld"

Output:

  Hello
  World

Using the -e Flag

Sometimes you need to force echo to recognize special characters. That’s where the -e flag comes in. The -e flag tells Linux to treat backslash escape sequences like \n (new line) or \t (tab). Sometimes we need to specify this flag if the echo command is not recognizing escape characters automatically. One such example is the Windows WSL. Go learn about it !! 😄

  echo -e "Hello\nWorld"

Output:

  Hello
  World

When using Linux on Windows (WSL), always use the -e flag with echo to handle escape sequences.


Event Designators: The Power of !!

Linux also has some neat shortcuts for commands you’ve already typed. One of the most useful is !!, which repeats the last command you ran. This can save a lot of time, especially if you forget to run something with admin (sudo) permissions.

Example: You run a command but forgot to use sudo:

  sudo !!

This will re-run your last command with sudo. It’s very handy when you don’t want to type the entire command again.


The printf Command in Linux

Now let’s talk about the printf command. It’s similar to echo but gives you more control over the way the output looks. For instance, with printf, you can format text, numbers, and even control the amount of space used.

Why Use printf?

  • Unlike echo, printf doesn’t automatically add a new line after the output. This gives you more control when formatting.
  • It allows you to control how numbers and strings are displayed.

Common Format Specifiers in printf

  • %s: Formats a value as a string (text).
  • %d: Formats a value as an integer (whole number).
  • %f: Formats a value as a floating-point number (decimals).
  • %.2f: Limits a floating-point number to 2 decimal places.
  • %10s: Pads a string to make it 10 characters long.
  • %10e: Formats a number in scientific notation (e.g., 1.23e+10).

Examples

  1. Basic String Formatting:
   printf "%s\n" "Hello World"

Output:

   Hello World
  1. Formatting Numbers:
   printf "%d\n" 42

Output:

   42
  1. Floating-Point Numbers:
   printf "%.2f\n" 42.123456

Output:

   42.12
  1. Padded Strings (adds extra spaces to make text line up nicely):
   printf "%10s\n" "Hello"

Output:

        Hello

The string Hello is right-aligned, with 5 spaces added to make it 10 characters long.

  1. Scientific Notation:
   printf "%10e\n" 12345.6789

Output:

   1.234568e+04

This shows the number 12345.6789 in scientific notation.


Wrapping Up

Learning how to work with standard input, output, and these commands will make you much more efficient in Linux. Commands like echo and printf might seem simple, but they are powerful tools that let you control how text and numbers are displayed. Plus, event designators like !! can save you time and effort.

Keep practicing these commands, and soon you’ll be zipping through Linux tasks like a pro hacker! Stay tuned for the next post in our Linux Basics for Hackers series.

Leave a Reply

Your email address will not be published. Required fields are marked *