Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to printf in bash with multiple arguments?

Tags:

bash

printf

How do I print a formatted system date + hostname using printf in bash.

Something like printf "%s:%s\n" date hostname?

like image 237
Jimson James Avatar asked Jun 05 '13 10:06

Jimson James


People also ask

How do I pass multiple arguments in bash?

You can pass more than one argument to your bash script. In general, here is the syntax of passing multiple arguments to any bash script: script.sh arg1 arg2 arg3 … The second argument will be referenced by the $2 variable, the third argument is referenced by $3 , .. etc.

Can I use printf in bash?

The bash printf command is a tool used for creating formatted output. It is a shell built-in, similar to the printf() function in C/C++, Java, PHP, and other programming languages. The command allows you to print formatted text and variables in standard output.

How do I print a statement in bash?

Typically, when writing bash scripts, we use echo to print to the standard output. echo 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.

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.


1 Answers

printf "%s:%s\n" "$(date)" "$(hostname)"
like image 177
jim mcnamara Avatar answered Sep 22 '22 06:09

jim mcnamara