Command not working properly in shell script
Here is output of:
sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null
Yash Shah
CoreFragment
CoreFragment_5G
CoreFragment
dlink
Yash Shah
COMFAST
Appbirds_Technologies
SYMBIOSIS
20096641
CoreFragment_5G
AMBER_AP1
REDWING LABS_5G
While the same thing written in a script is not working the same.
Here is a snippet in which I used above command.
for ssid_name in $(sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null)
do
echo "$ssid_name"
done
I got output like this:
Yash
Shah
CoreFragment
CoreFragment_5G
CoreFragment
Yash
Shah
REDWING
LABS
COMFAST
Appbirds_Technologies
SYMBIOSIS
CoreFragment_5G
REDWING
LABS_5G
Note: When there is a space in output it take as a new line.
I'm working on Ubuntu 18.04.
shell-script
add a comment |
Here is output of:
sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null
Yash Shah
CoreFragment
CoreFragment_5G
CoreFragment
dlink
Yash Shah
COMFAST
Appbirds_Technologies
SYMBIOSIS
20096641
CoreFragment_5G
AMBER_AP1
REDWING LABS_5G
While the same thing written in a script is not working the same.
Here is a snippet in which I used above command.
for ssid_name in $(sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null)
do
echo "$ssid_name"
done
I got output like this:
Yash
Shah
CoreFragment
CoreFragment_5G
CoreFragment
Yash
Shah
REDWING
LABS
COMFAST
Appbirds_Technologies
SYMBIOSIS
CoreFragment_5G
REDWING
LABS_5G
Note: When there is a space in output it take as a new line.
I'm working on Ubuntu 18.04.
shell-script
Not a duplicate, but the whitespace problem is discussed here: unix.stackexchange.com/q/9496/173368
– Haxiel
Jan 16 at 8:20
i didn't find that (might be due to word match) so I created question
– yash shah
Jan 16 at 8:23
No problem. I wanted to link to that QA because it contains several comprehensive answers that go into the details of that problem.
– Haxiel
Jan 16 at 8:25
yaa... it's really useful link
– yash shah
Jan 16 at 8:40
add a comment |
Here is output of:
sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null
Yash Shah
CoreFragment
CoreFragment_5G
CoreFragment
dlink
Yash Shah
COMFAST
Appbirds_Technologies
SYMBIOSIS
20096641
CoreFragment_5G
AMBER_AP1
REDWING LABS_5G
While the same thing written in a script is not working the same.
Here is a snippet in which I used above command.
for ssid_name in $(sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null)
do
echo "$ssid_name"
done
I got output like this:
Yash
Shah
CoreFragment
CoreFragment_5G
CoreFragment
Yash
Shah
REDWING
LABS
COMFAST
Appbirds_Technologies
SYMBIOSIS
CoreFragment_5G
REDWING
LABS_5G
Note: When there is a space in output it take as a new line.
I'm working on Ubuntu 18.04.
shell-script
Here is output of:
sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null
Yash Shah
CoreFragment
CoreFragment_5G
CoreFragment
dlink
Yash Shah
COMFAST
Appbirds_Technologies
SYMBIOSIS
20096641
CoreFragment_5G
AMBER_AP1
REDWING LABS_5G
While the same thing written in a script is not working the same.
Here is a snippet in which I used above command.
for ssid_name in $(sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null)
do
echo "$ssid_name"
done
I got output like this:
Yash
Shah
CoreFragment
CoreFragment_5G
CoreFragment
Yash
Shah
REDWING
LABS
COMFAST
Appbirds_Technologies
SYMBIOSIS
CoreFragment_5G
REDWING
LABS_5G
Note: When there is a space in output it take as a new line.
I'm working on Ubuntu 18.04.
shell-script
shell-script
edited Jan 16 at 18:23
wjandrea
474413
474413
asked Jan 16 at 8:01
yash shahyash shah
649
649
Not a duplicate, but the whitespace problem is discussed here: unix.stackexchange.com/q/9496/173368
– Haxiel
Jan 16 at 8:20
i didn't find that (might be due to word match) so I created question
– yash shah
Jan 16 at 8:23
No problem. I wanted to link to that QA because it contains several comprehensive answers that go into the details of that problem.
– Haxiel
Jan 16 at 8:25
yaa... it's really useful link
– yash shah
Jan 16 at 8:40
add a comment |
Not a duplicate, but the whitespace problem is discussed here: unix.stackexchange.com/q/9496/173368
– Haxiel
Jan 16 at 8:20
i didn't find that (might be due to word match) so I created question
– yash shah
Jan 16 at 8:23
No problem. I wanted to link to that QA because it contains several comprehensive answers that go into the details of that problem.
– Haxiel
Jan 16 at 8:25
yaa... it's really useful link
– yash shah
Jan 16 at 8:40
Not a duplicate, but the whitespace problem is discussed here: unix.stackexchange.com/q/9496/173368
– Haxiel
Jan 16 at 8:20
Not a duplicate, but the whitespace problem is discussed here: unix.stackexchange.com/q/9496/173368
– Haxiel
Jan 16 at 8:20
i didn't find that (might be due to word match) so I created question
– yash shah
Jan 16 at 8:23
i didn't find that (might be due to word match) so I created question
– yash shah
Jan 16 at 8:23
No problem. I wanted to link to that QA because it contains several comprehensive answers that go into the details of that problem.
– Haxiel
Jan 16 at 8:25
No problem. I wanted to link to that QA because it contains several comprehensive answers that go into the details of that problem.
– Haxiel
Jan 16 at 8:25
yaa... it's really useful link
– yash shah
Jan 16 at 8:40
yaa... it's really useful link
– yash shah
Jan 16 at 8:40
add a comment |
3 Answers
3
active
oldest
votes
First, note that it is not one command behaving differently. In your code snippets, standard output is written by two quite different commands.
Your first one prints sed
's output, while in your second one:
- The output of
sed
is substituted (by the command substitution$(...)
) afterin
; - The resulting string is expanded into a list of elements;
- Each item is in turn assigned to
ssid_name
and printed byecho
.
Expansion in point 2 is performed according to the value of the internal field separator (IFS
), which by default is <space><tab><newline>
. Thus, your solution works because it lets for
split the substituted string on newlines only.
As an aside on your answer, note that $'n'
is not portable - even if that syntax ($'string'
) is supported in many shells.
You can nevertheless use an assignment like this:
IFS='
'
An alternative construct for processing lines of input is using read
in a while
loop:
sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null |
while IFS= read -r i; do
printf '%sn' "$i"
done
Here, too, each line of sed
's output is split according to the value of IFS
- that in this example is set to the null string exactly to prevent any effects of word splitting - but lines would be kept separated by read
regardless of IFS
.
add a comment |
Better use a while
loop with read
instead of a for
loop.
sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null | while IFS= read -r ssid_name
do
echo $ssid_name
done
see also https://mywiki.wooledge.org/BashFAQ/001
add a comment |
By adding following line in code before for loop, work perfactly in my case.
IFS=$'n'
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});
}
});
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%2funix.stackexchange.com%2fquestions%2f494754%2fcommand-not-working-properly-in-shell-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
First, note that it is not one command behaving differently. In your code snippets, standard output is written by two quite different commands.
Your first one prints sed
's output, while in your second one:
- The output of
sed
is substituted (by the command substitution$(...)
) afterin
; - The resulting string is expanded into a list of elements;
- Each item is in turn assigned to
ssid_name
and printed byecho
.
Expansion in point 2 is performed according to the value of the internal field separator (IFS
), which by default is <space><tab><newline>
. Thus, your solution works because it lets for
split the substituted string on newlines only.
As an aside on your answer, note that $'n'
is not portable - even if that syntax ($'string'
) is supported in many shells.
You can nevertheless use an assignment like this:
IFS='
'
An alternative construct for processing lines of input is using read
in a while
loop:
sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null |
while IFS= read -r i; do
printf '%sn' "$i"
done
Here, too, each line of sed
's output is split according to the value of IFS
- that in this example is set to the null string exactly to prevent any effects of word splitting - but lines would be kept separated by read
regardless of IFS
.
add a comment |
First, note that it is not one command behaving differently. In your code snippets, standard output is written by two quite different commands.
Your first one prints sed
's output, while in your second one:
- The output of
sed
is substituted (by the command substitution$(...)
) afterin
; - The resulting string is expanded into a list of elements;
- Each item is in turn assigned to
ssid_name
and printed byecho
.
Expansion in point 2 is performed according to the value of the internal field separator (IFS
), which by default is <space><tab><newline>
. Thus, your solution works because it lets for
split the substituted string on newlines only.
As an aside on your answer, note that $'n'
is not portable - even if that syntax ($'string'
) is supported in many shells.
You can nevertheless use an assignment like this:
IFS='
'
An alternative construct for processing lines of input is using read
in a while
loop:
sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null |
while IFS= read -r i; do
printf '%sn' "$i"
done
Here, too, each line of sed
's output is split according to the value of IFS
- that in this example is set to the null string exactly to prevent any effects of word splitting - but lines would be kept separated by read
regardless of IFS
.
add a comment |
First, note that it is not one command behaving differently. In your code snippets, standard output is written by two quite different commands.
Your first one prints sed
's output, while in your second one:
- The output of
sed
is substituted (by the command substitution$(...)
) afterin
; - The resulting string is expanded into a list of elements;
- Each item is in turn assigned to
ssid_name
and printed byecho
.
Expansion in point 2 is performed according to the value of the internal field separator (IFS
), which by default is <space><tab><newline>
. Thus, your solution works because it lets for
split the substituted string on newlines only.
As an aside on your answer, note that $'n'
is not portable - even if that syntax ($'string'
) is supported in many shells.
You can nevertheless use an assignment like this:
IFS='
'
An alternative construct for processing lines of input is using read
in a while
loop:
sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null |
while IFS= read -r i; do
printf '%sn' "$i"
done
Here, too, each line of sed
's output is split according to the value of IFS
- that in this example is set to the null string exactly to prevent any effects of word splitting - but lines would be kept separated by read
regardless of IFS
.
First, note that it is not one command behaving differently. In your code snippets, standard output is written by two quite different commands.
Your first one prints sed
's output, while in your second one:
- The output of
sed
is substituted (by the command substitution$(...)
) afterin
; - The resulting string is expanded into a list of elements;
- Each item is in turn assigned to
ssid_name
and printed byecho
.
Expansion in point 2 is performed according to the value of the internal field separator (IFS
), which by default is <space><tab><newline>
. Thus, your solution works because it lets for
split the substituted string on newlines only.
As an aside on your answer, note that $'n'
is not portable - even if that syntax ($'string'
) is supported in many shells.
You can nevertheless use an assignment like this:
IFS='
'
An alternative construct for processing lines of input is using read
in a while
loop:
sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null |
while IFS= read -r i; do
printf '%sn' "$i"
done
Here, too, each line of sed
's output is split according to the value of IFS
- that in this example is set to the null string exactly to prevent any effects of word splitting - but lines would be kept separated by read
regardless of IFS
.
edited Jan 16 at 18:13
answered Jan 16 at 9:17
fra-sanfra-san
1,386214
1,386214
add a comment |
add a comment |
Better use a while
loop with read
instead of a for
loop.
sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null | while IFS= read -r ssid_name
do
echo $ssid_name
done
see also https://mywiki.wooledge.org/BashFAQ/001
add a comment |
Better use a while
loop with read
instead of a for
loop.
sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null | while IFS= read -r ssid_name
do
echo $ssid_name
done
see also https://mywiki.wooledge.org/BashFAQ/001
add a comment |
Better use a while
loop with read
instead of a for
loop.
sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null | while IFS= read -r ssid_name
do
echo $ssid_name
done
see also https://mywiki.wooledge.org/BashFAQ/001
Better use a while
loop with read
instead of a for
loop.
sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null | while IFS= read -r ssid_name
do
echo $ssid_name
done
see also https://mywiki.wooledge.org/BashFAQ/001
answered Jan 16 at 8:55
BodoBodo
5186
5186
add a comment |
add a comment |
By adding following line in code before for loop, work perfactly in my case.
IFS=$'n'
add a comment |
By adding following line in code before for loop, work perfactly in my case.
IFS=$'n'
add a comment |
By adding following line in code before for loop, work perfactly in my case.
IFS=$'n'
By adding following line in code before for loop, work perfactly in my case.
IFS=$'n'
answered Jan 16 at 8:07
yash shahyash shah
649
649
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f494754%2fcommand-not-working-properly-in-shell-script%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
Not a duplicate, but the whitespace problem is discussed here: unix.stackexchange.com/q/9496/173368
– Haxiel
Jan 16 at 8:20
i didn't find that (might be due to word match) so I created question
– yash shah
Jan 16 at 8:23
No problem. I wanted to link to that QA because it contains several comprehensive answers that go into the details of that problem.
– Haxiel
Jan 16 at 8:25
yaa... it's really useful link
– yash shah
Jan 16 at 8:40