Question about the meaning of backtick and %string [duplicate] - linux

This question already has answers here:
What is the benefit of using $() instead of backticks in shell scripts? [duplicate]
(9 answers)
YYYY-MM-DD format date in shell script
(17 answers)
Closed 4 years ago.
I am learning bash script and I am reading some sample code.
Anyone can advice me the detail and meaning of the following code? Thanks in advance!
END_DATE=`date -u "+%FT%TZ" `

The backticks are performing Command Sbustitution. The command inside the backticks is executed in a subshell and the result is assigned to the variable. (another syntax that would work the same in bash is $(command))
The string for the date command is how format is specified for the date command.

Related

How to execute text from stdout in bash [duplicate]

This question already has answers here:
Bash script - variable content as a command to run
(7 answers)
Closed 1 year ago.
So I have a command that's like
cmd1|cmd2|...|cmdN|execute
The output of cmdN is one line that I want execute to execute as if I copy pasted the output of cmdN into the terminal myself. I've tried to replace execute with bash, with $, and I tried to use xargs. (I'm still kinda confused on each of the options I've tried).
What's the simple answer here that's gonna make me wanna delete this post?
If you need to execute it in the current shell, use the eval command:
eval "$(cmd1|cmd2|...|cmdN)"
If it can be executed in a subshell, pipe to bash:
cmd1|cmd2|...|cmdN | bash

Bash Script: `cd` with command substitution [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Change the current directory from a Bash script
(17 answers)
How can I store a command in a variable in a shell script?
(12 answers)
Closed 2 years ago.
Summary:
I am trying to have a script that allows easy one-touch access to files that are generated by the crontab.
The hourly generated files go by the path as: /home/mouse/20210126/0900.
Of course as the date and time changes, the path will change like so:
/home/mouse/20210126/1000
/home/mouse/20210126/1100
/home/mouse/20210127/1000
/home/mouse/20210128/1300
I tried using an alias, but once .bashrc is loaded, it only takes the present date/time, which means that I cannot "refresh" the date and time.
So by using a script, I could update the date and time like so:
currentdate=$(date +%Y%m%d)
currenttime=$(date -d '1 hour ago' "+%H00")
collect=cd /home/mouse/$currentdate/$currenttime
echo $currentdate
echo $currenttime
$collect
However, I realized that the cd script is not working because, from my understanding, it works on a subshell and once the script has finished executing, it does not affect the main shell.
I tried source / . but I get /home/mouse/20210126/1000 is a directory.
What is the recommended action I should take to resolve this?

How to fix command not found in Linux shell scripting [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
why subtraction return - symbol
(2 answers)
Closed 2 years ago.
I have a Linux shell script with the below code
#! /bin/bash
echo $BASH
name = Mark
echo $name
When I run the script, I'm getting an error:
./my_script.sh: line 3: =: command not found
What am I doing wrong?
Note: I'm using Kali Linux.
In shell, you need to write:
echo $BASH
name=Mark
echo $name
Note there are no spaces around = when setting a variable. The shell usually interprets name = Mark as calling the command name with arguments =and Mark, hardly what you intend. It also seems that name somehow expands to nothing (an alias?), thus the confusing message about command =.

How to fetch output of command into a variable? [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 2 years ago.
I am trying to run a command and storing the values in a list
list = `sed -n 's/^abc//p' /etc/filename`
I am getting an error command not found while running the above command.
However, when I directly run the sed -n 's/^abc//p' /etc/filename command, the output is coming fine as below:
abc01 abc02 abc03
Use
list="$(sed -n 's/^abc//p' /etc/filename)"
There must be no spaces after variable declaration and equals sign. Also, quoting your variables is important.

Unable to print the public ip in shell script [duplicate]

This question already has answers here:
What is the difference between $(command) and `command` in shell programming?
(6 answers)
Closed 4 years ago.
Hi I am trying to print the public ip of the machine in a file using shell script. I am using the command
ip=${curl ipinfo.io/ip}
in my script file and it gives an error saying bad substitution. whereas this command works when i run it in command line.
Is this the right way to get the ip through the script?
Thanks in advance!!
You are capturing the result of program so you should use $(). The following should work for you (with the -s parameter to curl stopping unnecessary output)
ip=$(curl -s ipinfo.io/ip)

Resources