[LINUX] Shell script command replacement

Command replacement

When writing a shell script, you may want to use the output result of the `command in the shell script. `` You can use command substitution to ** get the result of a command as a string **.

How to Use

If you write the command you want to execute in parentheses in the format $ (), it will be replaced with the standard output when the shell script is executed.

Example: Display the current date in YYYY-MM-DD format

.sh


$ date '+%Y-%m-%d'
2020-05-04

Used for command replacement

date.sh


#!/bin/bash

filename=$(date '+%Y-%m-%d')
touch "$filename"

Doing this will create a file with the current date in YYYY-MM-DD format.

-rw-rw-r--  1 vagrant vagrant    0 May  4 10:54 2020-05-04

reference

New Linux textbook

Recommended Posts

Shell script command replacement
[Infrastructure] Linux command, shell script collection
Shell script numerical calculation bc command
Shell script basics # 2
Linux [shell command]
Shell script special variables
Shell script @ study memo
Let's try a shell script
Until the shell finds the command
Basic syntax notes for shell script
Run shell command / python in R
Automate environment construction with Shell Script
[Ubuntu] How to execute a shell script
Manage logrotate generations with a shell script.
Shell command to visualize line feed code
Creating a shell script to write a diary