Printf Command – In this tutorial, you will learn 5 practical examples of printf command in linux, printf command in linux with options, unix printf command with examples.
- It is a built-in bash command in linux
- It is used to display text, number or any other format specifier
- It is used to format and display its arguments like printf() function in c language
- Like cat and echo command, printf is a one of the used linux commands which is also used for file creation, displaying message, file copying, files merging, text appending to files, etc,.
- The following file operations can be done using printf command. They are
- File creation
- Text appending to file
- File copying
- Files merging
- Displaying file content to a variable.
General Syntax
1. File Creation using printf command
- Like cat command, a text file can be created using the printf command in linux / unix.
- This is done by using the output redirection operator >
- Use the below syntax to create a new file.
2. Text Appending to File using echo command
- Text can be appended to file using echo command along with output redirection append operator >>
- Use the below syntax to append text to an existing file.
3. File Copying using Printf command
- File can be copied to another file using printf command with command substitution method and output redirection operator >
- Command Substitution is a process of getting the output of built-in command to a variable. This is performed using $(command) or `command`
- Here the output of input file can be obtained using command substitution and these output will be redirected to target file using the output redirection operator >
4. Files Merging using Printf command
- This is done by using the same style of file copying
- So multiple files can be merged into single file using command substitution along with output redirection operator >
- Use the below syntax for files merging.
5. Displaying a file content to a variable
- It is possible to store the contents of file to a variable
- This is done by using the special option –v
- Use the below syntax to store the file content to a variable.
MORE TUTORIALS