Bash printf Command and Conversion specifications

Typically, when writing bash scripts, we use echo to print to the standard output. [echo](https://linuxize.com/post/echo-command-in-linux-with-examples/) is a simple command but is limited in its capabilities.

To have more control over the formatting of the output, use the printf command.

The printf command formats and prints its arguments, similar to the C printf() function.

printf Command

printf is a shell builtin in Bash and in other popular shells like Zsh and Ksh. There is also a standalone /usr/bin/printf binary, but the shell built-in version takes precedence. We will cover the Bash builtin version of printf.

The syntax for the printf command is as follows:

printf [-v var] format [arguments]

Copy

The -v option tells printf not to print the output but to assign it to the variable.

The format is a string which may contain three different types of objects:

  • Normal characters that are simply printed to the output as-is.
  • Backslash-escaped characters which are interpreted and then printed.
  • Conversion specifications that describe the format and are replaced by the values of respective arguments that follow the format string.

The command accepts any number of arguments. If more arguments than format specifiers are provided, the format string is reused to consume all of the arguments. If fewer arguments than format specifiers are supplied, the extra numeric-format specifiers are set to zero value while string-format specifiers are set to null string.

#bash #printf #echo

What is GEEK

Buddha Community

Bash printf Command and Conversion specifications

Bash printf Command and Conversion specifications

Typically, when writing bash scripts, we use echo to print to the standard output. [echo](https://linuxize.com/post/echo-command-in-linux-with-examples/) is a simple command but is limited in its capabilities.

To have more control over the formatting of the output, use the printf command.

The printf command formats and prints its arguments, similar to the C printf() function.

printf Command

printf is a shell builtin in Bash and in other popular shells like Zsh and Ksh. There is also a standalone /usr/bin/printf binary, but the shell built-in version takes precedence. We will cover the Bash builtin version of printf.

The syntax for the printf command is as follows:

printf [-v var] format [arguments]

Copy

The -v option tells printf not to print the output but to assign it to the variable.

The format is a string which may contain three different types of objects:

  • Normal characters that are simply printed to the output as-is.
  • Backslash-escaped characters which are interpreted and then printed.
  • Conversion specifications that describe the format and are replaced by the values of respective arguments that follow the format string.

The command accepts any number of arguments. If more arguments than format specifiers are provided, the format string is reused to consume all of the arguments. If fewer arguments than format specifiers are supplied, the extra numeric-format specifiers are set to zero value while string-format specifiers are set to null string.

#bash #printf #echo

Jackson  Watson

Jackson Watson

1625660880

Bash printf: don't use echo! Beyond Code Live 006.c

Rather than teaching you every tool, I try to teach just the best tools.
I was wrong to teach you echo, which is useless, when I could have taught you printf, which is very useful.
But tonight I’m correcting that great miscarriage of justice!

printf ‘’ [double arrow] ./filename.txt # create a file
printf ‘Hello World!\n’ [double greater than] ./filename.txt # Add the line ‘Hello, World!’
printf ‘%q’ ‘Hello World!’ # show acceptable escapes

Beyond Code:
(Learn to Code in 15 Minutes a Day)
Bootcamp Playlist: https://www.youtube.com/playlist?list=PLxki0D-ilnqZfyo2dZe11ZNGP7RJxJcoA
Subscribe on YouTube: https://youtube.com/channel/UC2KJHARTj6KRpKzLU1sVxBA
Join on Facebook: https://fb.com/beyondcodebootcamp
Follow on Twitter: https://twitter.com/@_beyondcode

AJ’s Live Streams:
Watch on Twitch: https://twitch.tv/coolaj86
Watch on Facebook: https://facebook.com/coolaj86
Subscribe on YouTube: https://youtube.com/coolaj86

Health, Wealth, Commitment
(My Morning Shower Thoughts as a Daily Lifestyle Vlog)
Join on Facebook: https://www.facebook.com/groups/5406824179391158
Subscribe on YouTube: https://www.youtube.com/channel/UCbw2SbqD0OofAEVF_T61wCQ

#softwaredevelopment #softwareengineer #webdevelopment #webdeveloper

#bash #softwareengineer #webdevelopment #bash printf #webdeveloper #echo

Annalise  Hyatt

Annalise Hyatt

1595303760

Bash Select (Make Menus) and Bash select Example

In this tutorial, we will cover the basics of the select construct in Bash.

The select construct allows you to generate menus.

Bash select Construct

The select construct generates a menu from a list of items. It has almost the same syntax as the [for](https://linuxize.com/post/bash-for-loop/) loop:

select ITEM in [LIST]
do
  [COMMANDS]
done

Copy

The [LIST] can be a series of strings separated by spaces, a range of numbers, output of a command, an array, and so on. A custom prompt for the select construct can be set using the PS3 environment variable.

When the select construct is invoked, each item from the list is printed on the screen (standard error), preceded with a number.

If the user enters a number that corresponds to the number of one of the displayed items, then the value of [ITEM] is set to that item. The value of the selected item is stored in the variable REPLY. Otherwise, if the user input is empty, the prompt and the menu list are displayed again.

Theselectloop will continue to run and prompt for user input until the[break](https://linuxize.com/post/bash-break-continue/)command is executed.To demonstrate how the select construct works, let’s take a look at the following simple example:

PS3="Enter a number: "

select character in Sheldon Leonard Penny Howard Raj
do
    echo "Selected character: $character"
    echo "Selected number: $REPLY"
done

#bash #bash select construct #bash select

Examples of the dig command in Linux

Dig Command Line Options and Examples
Here is the frequently used command line options and example’s of dig command.
1. Basic Dig Command
A basic dig command accept domain name as command line parameter and prints Address record.
2. Query With Specific DNS Server
The default dig command queries to dns server configured on your system. For example, the Linux systems keep default DNS entry in /etc/resolv.conf.
3. Print Short Answer
Use +short command line option to print result in short form. This is basically useful with the shell scripting and other automation tasks.
4. Print Detailed but Specific Result
Use +noall with +answer to print detailed information but specific. This will print only answer section including few more details as a result.

#linux commands #command #dig #dig command #useful examples #linux

Useful Example for the mv command in Linux

mv Command Examples
Below is the basic mv command examples on Linux terminal.

  • Rename file in current directory – For example rename file source.txt to dest.txt in current directory.
  • Move file to destination directory – Move a source.txt file available in current directory to /tmp directory. In above command the file name is unchanged at target directory. You can also rename file name at destination directory just by providing filename at destination.
  • Move file from source directory to destination directory – We can also move files or directory from some other directory to destination directory.
  • Move multiple files to destination at once – The mv accepts multiple source files and move them to the destination directory at once. The following command will move file1.txt, file2.txt in current directory and /opt/file3.txt to the /tmp directory. Here the last command line parameter is used as destination by the mv command.

#linux commands #command #mv #useful example #mv command #linux