Assign output to variable in Bash
In shell, you don't put a $ in front of a variable you're assigning. You only use $IP when you're referring to the variable.
#!/bin/bash
IP=$(curl automation.whatismyip.com/n09230945.asp)
echo "$IP"
sed "s/IP/$IP/" nsupdate.txt | nsupdate
In ShellScript Assign Variable Based on Curl Output
There should no have spaces when assign url, try this :-
url=$(curl -s ...)
note : curl -s is to suppress the progress meter
Saving cURL -w variables to a bash variable
It's not possible just like that coz every command run in its own subshell, create own vars and so on. Try this code:
export somevar=test; curl -kso /dev/null -w "$(export somevar=%{time_connect}; env | grep somevar)\n" https://www.google.com; env | grep somevar
The output(in my case) will be:
somevar=0,004898
somevar=test
So the way to do it is to output directly to var.
somevar=$(curl -kso /dev/null -w "%{time_connect}" https://www.google.com)
$ echo $somevar
0,005093
Or to an array, if you need multiple items.
somearray=( $(curl -kso /dev/null -w "%{time_connect} %{time_namelookup} %{time_appconnect}" https://www.google.com) )
$ echo ${somearray[@]}
0,005095 0,004317 0,165347
How to capture the output of curl to variable in bash
You have two options (see this StackOverflow answer here):
- Preferred: Surround the invocation in
$()
- Surround the invocation in back ticks
NOTE: back ticks are legacy, the former method is preferred.
output=$(curl -I http://google.com | head -n 1| cut -d $' ' -f2)
echo "$output";
output=`curl -I http://google.com | head -n 1| cut -d $' ' -f2`
echo "$output";
Shell script -- how to store curl command WITH variables result into another variable?
This is the command in your command substitution:
curl -XGET '${search_host}/${index}/_mapping'
This command will not work on its own. The problem that you are observing is due to the single quotes, not to the use of command substitution, $(...)
.
Inside single-quotes, variables are not expanded. Since you need variable expansion, use double-quotes:
curl -XGET "${search_host}/${index}/_mapping"
Putting the above in your original command:
local sample_mapping="$(curl -XGET "${search_host}/${index}/_mapping")"
Note that quotes and command substitutions can nest : the fact that there are double quotes inside the command substitution has no effect on the double quotes outside of the command substitution.
Example
Let's define some variables:
$ search_host=http://google.com
$ index=index.html
Now, let's try the command with single-quotes:
$ a=$(curl -XGET '$search_host/$index')
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (6) Could not resolve host: $search_host
The above failed with an error message similar to the one you observed: Could not resolve host: $search_host
Let's try again with double-quotes:
$ a=$(curl -XGET "$search_host/$index")
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 229 100
229 0 0 520 0 --:--:-- --:--:-- --:--:-- 521
The above succeeded.
Related Topics
How to Generate Multiple Ssh Public Key and Configure Those on Windows Machine from Gitbash
Tcp Keepalive - Protocol Not Available
How to Get Argv from "Struct Linux_Binprm"
Session Permission Denied on Aws After Setting Up Cake PHP.
Linux History of All Commands Executed During Whole Day, Everyday
Perl and Bash Variable Substitution, with Hexadecimal Characters and Repeat Count
How to Simulate a Usb Printer to Lpt on Linux
How to Clone a Git Repository Which Is Present on a Remote Linux Server into Windows
Cannot Find Module 'Firebase-Admin' When Trying to Deploy Firebase Functions
Bash Command to Search for Any Occurrence of Phrase and Return List of Files and Paths
How to Enable or Configure Ftrace Module
How to Print Multiple Variables Using Printf
How to Strip Down My Yocto Linux
Run a Script When a New Veth Interface Is Added
How to Get The File System Type for Syscall.Mount() Programmatically