Variable doesn't parse as stringCan you help me to understand this explanation of shell quoting?Alternatives for [variable = string substitution] in bashScript to extract text using grepBash script — store `curl` output in variable, then format against string in variablebash script to run a second command with select output from first command as variableHow to get BASH to use * wildcard in command?Extract specific field in matrixgnome-terminal hides ending characters of the current path's first rowParse json object to bashVariable for absolute path and Alias for a command in one string or scrip?
Variable doesn't parse as string
Write The Shortest Program to Calculate Height of a Binary Tree
split inside flalign
How do people drown while wearing a life jacket?
The Game of the Century - why didn't Byrne take the rook after he forked Fischer?
Is unspent vacation time a good argument in pay negotiations?
Why do proponents of guns oppose gun competency tests?
Formal mathematical definition of renormalization group flow
Upper Bound for a Sum
Why does capacitance not depend on the material of the plates?
How to increase Solr JVM memory
Did Logical Positivism fail because it simply denied human emotion?
Need reasons why a satellite network would not work
Broken bottom bracket?
Is it okay to use different fingers every time while playing a song on keyboard? Is it considered a bad practice?
Would the shaking of an earthquake be visible to somebody in a low-flying aircraft?
Conditional probability of dependent random variables
ZFS on Linux: Which mountpoint option when mounting manually per script?
Is there a general term for the items in a directory?
What's "halachic" about "Esav hates Ya'akov"?
Is there a booking app or site that lets you specify your gender for shared dormitories?
How to call made-up data?
Why did the US Airways Flight 1549 passengers stay on the wings?
On the consistency of different well-polished astronomy software
Variable doesn't parse as string
Can you help me to understand this explanation of shell quoting?Alternatives for [variable = string substitution] in bashScript to extract text using grepBash script — store `curl` output in variable, then format against string in variablebash script to run a second command with select output from first command as variableHow to get BASH to use * wildcard in command?Extract specific field in matrixgnome-terminal hides ending characters of the current path's first rowParse json object to bashVariable for absolute path and Alias for a command in one string or scrip?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Problem
I tried to extract the signal level for wifi in the following way, in a bash script (quotes seem to make no difference):
string="$(iwconfig wlan0 | grep -I Signal)"
If I echo $string
, I get what I expect:
Link Quality=63/70 Signal level=-47dBm
But if I try to get a substring, echo $string:5
, it returns the same thing.
Debuging:
If I paste the output of wconfig wlan0 | grep -I Signal
directly into the variable: string="Link Quality=63/70 Signal level=-47dBm"
then everything works as expected.
echo $string
# Link Quality=63/70 Signal level=-47dBm
echo $string:5
# Quality=63/70 Signal level=-47dBm
Question: According to the internet, all bash variables are stored as character strings. Thus, the debugging result should have been the same as my original problem. Is there some reason it is not parsed as text in the original problem?
command-line bash scripts text
New contributor
add a comment |
Problem
I tried to extract the signal level for wifi in the following way, in a bash script (quotes seem to make no difference):
string="$(iwconfig wlan0 | grep -I Signal)"
If I echo $string
, I get what I expect:
Link Quality=63/70 Signal level=-47dBm
But if I try to get a substring, echo $string:5
, it returns the same thing.
Debuging:
If I paste the output of wconfig wlan0 | grep -I Signal
directly into the variable: string="Link Quality=63/70 Signal level=-47dBm"
then everything works as expected.
echo $string
# Link Quality=63/70 Signal level=-47dBm
echo $string:5
# Quality=63/70 Signal level=-47dBm
Question: According to the internet, all bash variables are stored as character strings. Thus, the debugging result should have been the same as my original problem. Is there some reason it is not parsed as text in the original problem?
command-line bash scripts text
New contributor
add a comment |
Problem
I tried to extract the signal level for wifi in the following way, in a bash script (quotes seem to make no difference):
string="$(iwconfig wlan0 | grep -I Signal)"
If I echo $string
, I get what I expect:
Link Quality=63/70 Signal level=-47dBm
But if I try to get a substring, echo $string:5
, it returns the same thing.
Debuging:
If I paste the output of wconfig wlan0 | grep -I Signal
directly into the variable: string="Link Quality=63/70 Signal level=-47dBm"
then everything works as expected.
echo $string
# Link Quality=63/70 Signal level=-47dBm
echo $string:5
# Quality=63/70 Signal level=-47dBm
Question: According to the internet, all bash variables are stored as character strings. Thus, the debugging result should have been the same as my original problem. Is there some reason it is not parsed as text in the original problem?
command-line bash scripts text
New contributor
Problem
I tried to extract the signal level for wifi in the following way, in a bash script (quotes seem to make no difference):
string="$(iwconfig wlan0 | grep -I Signal)"
If I echo $string
, I get what I expect:
Link Quality=63/70 Signal level=-47dBm
But if I try to get a substring, echo $string:5
, it returns the same thing.
Debuging:
If I paste the output of wconfig wlan0 | grep -I Signal
directly into the variable: string="Link Quality=63/70 Signal level=-47dBm"
then everything works as expected.
echo $string
# Link Quality=63/70 Signal level=-47dBm
echo $string:5
# Quality=63/70 Signal level=-47dBm
Question: According to the internet, all bash variables are stored as character strings. Thus, the debugging result should have been the same as my original problem. Is there some reason it is not parsed as text in the original problem?
command-line bash scripts text
command-line bash scripts text
New contributor
New contributor
edited 8 hours ago
user3140225
6633 silver badges17 bronze badges
6633 silver badges17 bronze badges
New contributor
asked 8 hours ago
Ion SmeIon Sme
334 bronze badges
334 bronze badges
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
This is because echo $string
doesn't simply print the value of string
; it first perform a split+glob operation - one of the consequences of which is that leading whitespace is elided:
$ string=$(iwconfig wls1 | grep Signal)
$ echo $string
Link Quality=38/70 Signal level=-72 dBm
whereas
$ echo "$string"
Link Quality=38/70 Signal level=-72 dBm
We can see that there is a sequence of whitespace characters at the start of $string
- in fact there are more than 5, so removing 5 still leaves a string with leading whitespace, which an unquoted echo
also elides:
$ echo "$string:5"
Link Quality=38/70 Signal level=-72 dBm
$ echo $string:5
Link Quality=38/70 Signal level=-72 dBm
For additional discussion see:
When is double-quoting necessary?
Why does my shell script choke on whitespace or other special characters?
add a comment |
This works on my machine:
$ string="$(iwconfig wlp60s0 | grep -I Signal)"
$ echo $string
Link Quality=68/70 Signal level=-42 dBm
$ echo $string | cut -d' ' -f4,5
level=-42 dBm
- For your machine replace
wlp60s0
withwlan0
.
Note this is using Ubuntu 16.04 and Ubuntu 19.04 where there is a space between42
anddBm
. As such thecut
command is instructed to print fields #4 and #5. In your question there is no space so I'm not sure what version you are using.
You could also use:
$ echo $string | cut -d'=' -f3
-42 dBm
- In this case
cut
is told to use=
as field delimiter.
If you want to use $string...
though the correct syntax is:
$ echo $string##*=
-38 dBm
$ echo "$string##*="
-38 dBm
Either method will work to take the substring after the last =
. The original method of 5
in your question I don't understand how it can work.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Ion Sme is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1163602%2fvariable-doesnt-parse-as-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is because echo $string
doesn't simply print the value of string
; it first perform a split+glob operation - one of the consequences of which is that leading whitespace is elided:
$ string=$(iwconfig wls1 | grep Signal)
$ echo $string
Link Quality=38/70 Signal level=-72 dBm
whereas
$ echo "$string"
Link Quality=38/70 Signal level=-72 dBm
We can see that there is a sequence of whitespace characters at the start of $string
- in fact there are more than 5, so removing 5 still leaves a string with leading whitespace, which an unquoted echo
also elides:
$ echo "$string:5"
Link Quality=38/70 Signal level=-72 dBm
$ echo $string:5
Link Quality=38/70 Signal level=-72 dBm
For additional discussion see:
When is double-quoting necessary?
Why does my shell script choke on whitespace or other special characters?
add a comment |
This is because echo $string
doesn't simply print the value of string
; it first perform a split+glob operation - one of the consequences of which is that leading whitespace is elided:
$ string=$(iwconfig wls1 | grep Signal)
$ echo $string
Link Quality=38/70 Signal level=-72 dBm
whereas
$ echo "$string"
Link Quality=38/70 Signal level=-72 dBm
We can see that there is a sequence of whitespace characters at the start of $string
- in fact there are more than 5, so removing 5 still leaves a string with leading whitespace, which an unquoted echo
also elides:
$ echo "$string:5"
Link Quality=38/70 Signal level=-72 dBm
$ echo $string:5
Link Quality=38/70 Signal level=-72 dBm
For additional discussion see:
When is double-quoting necessary?
Why does my shell script choke on whitespace or other special characters?
add a comment |
This is because echo $string
doesn't simply print the value of string
; it first perform a split+glob operation - one of the consequences of which is that leading whitespace is elided:
$ string=$(iwconfig wls1 | grep Signal)
$ echo $string
Link Quality=38/70 Signal level=-72 dBm
whereas
$ echo "$string"
Link Quality=38/70 Signal level=-72 dBm
We can see that there is a sequence of whitespace characters at the start of $string
- in fact there are more than 5, so removing 5 still leaves a string with leading whitespace, which an unquoted echo
also elides:
$ echo "$string:5"
Link Quality=38/70 Signal level=-72 dBm
$ echo $string:5
Link Quality=38/70 Signal level=-72 dBm
For additional discussion see:
When is double-quoting necessary?
Why does my shell script choke on whitespace or other special characters?
This is because echo $string
doesn't simply print the value of string
; it first perform a split+glob operation - one of the consequences of which is that leading whitespace is elided:
$ string=$(iwconfig wls1 | grep Signal)
$ echo $string
Link Quality=38/70 Signal level=-72 dBm
whereas
$ echo "$string"
Link Quality=38/70 Signal level=-72 dBm
We can see that there is a sequence of whitespace characters at the start of $string
- in fact there are more than 5, so removing 5 still leaves a string with leading whitespace, which an unquoted echo
also elides:
$ echo "$string:5"
Link Quality=38/70 Signal level=-72 dBm
$ echo $string:5
Link Quality=38/70 Signal level=-72 dBm
For additional discussion see:
When is double-quoting necessary?
Why does my shell script choke on whitespace or other special characters?
answered 8 hours ago
steeldriversteeldriver
77.2k12 gold badges128 silver badges206 bronze badges
77.2k12 gold badges128 silver badges206 bronze badges
add a comment |
add a comment |
This works on my machine:
$ string="$(iwconfig wlp60s0 | grep -I Signal)"
$ echo $string
Link Quality=68/70 Signal level=-42 dBm
$ echo $string | cut -d' ' -f4,5
level=-42 dBm
- For your machine replace
wlp60s0
withwlan0
.
Note this is using Ubuntu 16.04 and Ubuntu 19.04 where there is a space between42
anddBm
. As such thecut
command is instructed to print fields #4 and #5. In your question there is no space so I'm not sure what version you are using.
You could also use:
$ echo $string | cut -d'=' -f3
-42 dBm
- In this case
cut
is told to use=
as field delimiter.
If you want to use $string...
though the correct syntax is:
$ echo $string##*=
-38 dBm
$ echo "$string##*="
-38 dBm
Either method will work to take the substring after the last =
. The original method of 5
in your question I don't understand how it can work.
add a comment |
This works on my machine:
$ string="$(iwconfig wlp60s0 | grep -I Signal)"
$ echo $string
Link Quality=68/70 Signal level=-42 dBm
$ echo $string | cut -d' ' -f4,5
level=-42 dBm
- For your machine replace
wlp60s0
withwlan0
.
Note this is using Ubuntu 16.04 and Ubuntu 19.04 where there is a space between42
anddBm
. As such thecut
command is instructed to print fields #4 and #5. In your question there is no space so I'm not sure what version you are using.
You could also use:
$ echo $string | cut -d'=' -f3
-42 dBm
- In this case
cut
is told to use=
as field delimiter.
If you want to use $string...
though the correct syntax is:
$ echo $string##*=
-38 dBm
$ echo "$string##*="
-38 dBm
Either method will work to take the substring after the last =
. The original method of 5
in your question I don't understand how it can work.
add a comment |
This works on my machine:
$ string="$(iwconfig wlp60s0 | grep -I Signal)"
$ echo $string
Link Quality=68/70 Signal level=-42 dBm
$ echo $string | cut -d' ' -f4,5
level=-42 dBm
- For your machine replace
wlp60s0
withwlan0
.
Note this is using Ubuntu 16.04 and Ubuntu 19.04 where there is a space between42
anddBm
. As such thecut
command is instructed to print fields #4 and #5. In your question there is no space so I'm not sure what version you are using.
You could also use:
$ echo $string | cut -d'=' -f3
-42 dBm
- In this case
cut
is told to use=
as field delimiter.
If you want to use $string...
though the correct syntax is:
$ echo $string##*=
-38 dBm
$ echo "$string##*="
-38 dBm
Either method will work to take the substring after the last =
. The original method of 5
in your question I don't understand how it can work.
This works on my machine:
$ string="$(iwconfig wlp60s0 | grep -I Signal)"
$ echo $string
Link Quality=68/70 Signal level=-42 dBm
$ echo $string | cut -d' ' -f4,5
level=-42 dBm
- For your machine replace
wlp60s0
withwlan0
.
Note this is using Ubuntu 16.04 and Ubuntu 19.04 where there is a space between42
anddBm
. As such thecut
command is instructed to print fields #4 and #5. In your question there is no space so I'm not sure what version you are using.
You could also use:
$ echo $string | cut -d'=' -f3
-42 dBm
- In this case
cut
is told to use=
as field delimiter.
If you want to use $string...
though the correct syntax is:
$ echo $string##*=
-38 dBm
$ echo "$string##*="
-38 dBm
Either method will work to take the substring after the last =
. The original method of 5
in your question I don't understand how it can work.
edited 7 hours ago
answered 8 hours ago
WinEunuuchs2UnixWinEunuuchs2Unix
56.1k16 gold badges109 silver badges217 bronze badges
56.1k16 gold badges109 silver badges217 bronze badges
add a comment |
add a comment |
Ion Sme is a new contributor. Be nice, and check out our Code of Conduct.
Ion Sme is a new contributor. Be nice, and check out our Code of Conduct.
Ion Sme is a new contributor. Be nice, and check out our Code of Conduct.
Ion Sme is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1163602%2fvariable-doesnt-parse-as-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown