BASH variables

FBClark

New Member
Joined
Oct 19, 2020
Messages
19
Reaction score
19
Credits
287
ls /sys/class/net | awk '{print $1}' | tail -n1
ls /sys/class/net | awk '{print $1}' | head -n1
yeilds
wlp3s0
eno1
How to make these variables? I've tried single quotes and double quotes around the whole. This is basically what I want;
wirelessname=ls /sys/class/net | awk '{print $1}' | tail -n1
wiredname=ls /sys/class/net | awk '{print $1}' | head -n1
echo "Wireless is ${wirelessname}"
echo "Wired is ${wiredname}"
Debian 12 on a Dell laptop, home network.
 


ls /sys/class/net | awk '{print $1}' | tail -n1
ls /sys/class/net | awk '{print $1}' | head -n1
yeilds
wlp3s0
eno1
How to make these variables? I've tried single quotes and double quotes around the whole. This is basically what I want;
wirelessname=ls /sys/class/net | awk '{print $1}' | tail -n1
wiredname=ls /sys/class/net | awk '{print $1}' | head -n1
echo "Wireless is ${wirelessname}"
echo "Wired is ${wiredname}"
Debian 12 on a Dell laptop, home network.
Hey dude! To assign the output of a command to a variable, you should use command substitution. You're on the right track, but instead of just typing the command inside the variable assignment, you need to use the $() syntax. Here's the correct way to do it:

Bash:
wirelessname=$(ls /sys/class/net | awk '{print $1}' | tail -n1)
wiredname=$(ls /sys/class/net | awk '{print $1}' | head -n1)

echo "Wireless is ${wirelessname}"
echo "Wired is ${wiredname}"

By using $(), the shell will execute the command within the parentheses and assign its output to the variable. Now, when you echo the variables, they should contain the expected values.

Enjoy your Debian 12 on your Dell laptop, and happy coding, my friend!
 
Hey dude! To assign the output of a command to a variable, you should use command substitution. You're on the right track, but instead of just typing the command inside the variable assignment, you need to use the $() syntax. Here's the correct way to do it:

Bash:
wirelessname=$(ls /sys/class/net | awk '{print $1}' | tail -n1)
wiredname=$(ls /sys/class/net | awk '{print $1}' | head -n1)

echo "Wireless is ${wirelessname}"
echo "Wired is ${wiredname}"

By using $(), the shell will execute the command within the parentheses and assign its output to the variable. Now, when you echo the variables, they should contain the expected values.

Enjoy your Debian 12 on your Dell laptop, and happy coding, my friend!
Totally Tubular man! Thanks!
 

Members online


Top