Why there is a difference between the output of 'echo $VAR | wc -c' and 'echo $#VAR'?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I'm working on a Bash script and the length of the string contained in a certain variable is one of my conditions. The current string is W5u7TBTzF17GGFV8DBJHvgAAAAI
. Initially I've count the string length by the help of wc -c
:
$ echo 'W5u7TBTzF17GGFV8DBJHvgAAAAI' | wc -c
28
$ VAR='W5u7TBTzF17GGFV8DBJHvgAAAAI'
$ echo "$VAR" | wc -c
28
But my script condition [[ $#VAR -eq 28 ]]
never pass. Then I decided to count the characters on by one. And actually the string length is 27 characters:
W5u7TBTzF17GGFV8DBJHvgAAAAI
---------------------------
123456789012345678901234567
1 2
Also the value of $#VAR
is 27:
$ echo "$#VAR"
27
So I'm in wondering from where this difference comes from?
command-line bash scripts echo wc
add a comment |Â
up vote
1
down vote
favorite
I'm working on a Bash script and the length of the string contained in a certain variable is one of my conditions. The current string is W5u7TBTzF17GGFV8DBJHvgAAAAI
. Initially I've count the string length by the help of wc -c
:
$ echo 'W5u7TBTzF17GGFV8DBJHvgAAAAI' | wc -c
28
$ VAR='W5u7TBTzF17GGFV8DBJHvgAAAAI'
$ echo "$VAR" | wc -c
28
But my script condition [[ $#VAR -eq 28 ]]
never pass. Then I decided to count the characters on by one. And actually the string length is 27 characters:
W5u7TBTzF17GGFV8DBJHvgAAAAI
---------------------------
123456789012345678901234567
1 2
Also the value of $#VAR
is 27:
$ echo "$#VAR"
27
So I'm in wondering from where this difference comes from?
command-line bash scripts echo wc
1
Thisecho -n $VAR | wc -c
give27
, so newline character is there
â George Udosen
54 mins ago
1
@GeorgeUdosen, probably this should be the answer :)
â pa4080
49 mins ago
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm working on a Bash script and the length of the string contained in a certain variable is one of my conditions. The current string is W5u7TBTzF17GGFV8DBJHvgAAAAI
. Initially I've count the string length by the help of wc -c
:
$ echo 'W5u7TBTzF17GGFV8DBJHvgAAAAI' | wc -c
28
$ VAR='W5u7TBTzF17GGFV8DBJHvgAAAAI'
$ echo "$VAR" | wc -c
28
But my script condition [[ $#VAR -eq 28 ]]
never pass. Then I decided to count the characters on by one. And actually the string length is 27 characters:
W5u7TBTzF17GGFV8DBJHvgAAAAI
---------------------------
123456789012345678901234567
1 2
Also the value of $#VAR
is 27:
$ echo "$#VAR"
27
So I'm in wondering from where this difference comes from?
command-line bash scripts echo wc
I'm working on a Bash script and the length of the string contained in a certain variable is one of my conditions. The current string is W5u7TBTzF17GGFV8DBJHvgAAAAI
. Initially I've count the string length by the help of wc -c
:
$ echo 'W5u7TBTzF17GGFV8DBJHvgAAAAI' | wc -c
28
$ VAR='W5u7TBTzF17GGFV8DBJHvgAAAAI'
$ echo "$VAR" | wc -c
28
But my script condition [[ $#VAR -eq 28 ]]
never pass. Then I decided to count the characters on by one. And actually the string length is 27 characters:
W5u7TBTzF17GGFV8DBJHvgAAAAI
---------------------------
123456789012345678901234567
1 2
Also the value of $#VAR
is 27:
$ echo "$#VAR"
27
So I'm in wondering from where this difference comes from?
command-line bash scripts echo wc
command-line bash scripts echo wc
asked 1 hour ago
pa4080
12.2k52256
12.2k52256
1
Thisecho -n $VAR | wc -c
give27
, so newline character is there
â George Udosen
54 mins ago
1
@GeorgeUdosen, probably this should be the answer :)
â pa4080
49 mins ago
add a comment |Â
1
Thisecho -n $VAR | wc -c
give27
, so newline character is there
â George Udosen
54 mins ago
1
@GeorgeUdosen, probably this should be the answer :)
â pa4080
49 mins ago
1
1
This
echo -n $VAR | wc -c
give 27
, so newline character is thereâ George Udosen
54 mins ago
This
echo -n $VAR | wc -c
give 27
, so newline character is thereâ George Udosen
54 mins ago
1
1
@GeorgeUdosen, probably this should be the answer :)
â pa4080
49 mins ago
@GeorgeUdosen, probably this should be the answer :)
â pa4080
49 mins ago
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
5
down vote
accepted
It's the way echo
works. Now do
echo koko
You get
georgek@georgek-HP-Pavilion-17-Notebook-PC:~$ echo koko
koko
But do echo -n koko
and you get
georgek@georgek-HP-Pavilion-17-Notebook-PC:~$ echo -n koko
kokogeorgek@georgek-HP-Pavilion-17-Notebook-PC:~$
So wc
is capturing the newline character
too. Use
echo -n $VAR | wc -c
To get the desired result. The echo
command will add the newline
character, so that gets counted too. To remove this and get the real count use the -n
option.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
It's the way echo
works. Now do
echo koko
You get
georgek@georgek-HP-Pavilion-17-Notebook-PC:~$ echo koko
koko
But do echo -n koko
and you get
georgek@georgek-HP-Pavilion-17-Notebook-PC:~$ echo -n koko
kokogeorgek@georgek-HP-Pavilion-17-Notebook-PC:~$
So wc
is capturing the newline character
too. Use
echo -n $VAR | wc -c
To get the desired result. The echo
command will add the newline
character, so that gets counted too. To remove this and get the real count use the -n
option.
add a comment |Â
up vote
5
down vote
accepted
It's the way echo
works. Now do
echo koko
You get
georgek@georgek-HP-Pavilion-17-Notebook-PC:~$ echo koko
koko
But do echo -n koko
and you get
georgek@georgek-HP-Pavilion-17-Notebook-PC:~$ echo -n koko
kokogeorgek@georgek-HP-Pavilion-17-Notebook-PC:~$
So wc
is capturing the newline character
too. Use
echo -n $VAR | wc -c
To get the desired result. The echo
command will add the newline
character, so that gets counted too. To remove this and get the real count use the -n
option.
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
It's the way echo
works. Now do
echo koko
You get
georgek@georgek-HP-Pavilion-17-Notebook-PC:~$ echo koko
koko
But do echo -n koko
and you get
georgek@georgek-HP-Pavilion-17-Notebook-PC:~$ echo -n koko
kokogeorgek@georgek-HP-Pavilion-17-Notebook-PC:~$
So wc
is capturing the newline character
too. Use
echo -n $VAR | wc -c
To get the desired result. The echo
command will add the newline
character, so that gets counted too. To remove this and get the real count use the -n
option.
It's the way echo
works. Now do
echo koko
You get
georgek@georgek-HP-Pavilion-17-Notebook-PC:~$ echo koko
koko
But do echo -n koko
and you get
georgek@georgek-HP-Pavilion-17-Notebook-PC:~$ echo -n koko
kokogeorgek@georgek-HP-Pavilion-17-Notebook-PC:~$
So wc
is capturing the newline character
too. Use
echo -n $VAR | wc -c
To get the desired result. The echo
command will add the newline
character, so that gets counted too. To remove this and get the real count use the -n
option.
answered 48 mins ago
George Udosen
17.1k93660
17.1k93660
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1075384%2fwhy-there-is-a-difference-between-the-output-of-echo-var-wc-c-and-echo%23new-answer', 'question_page');
);
Post as a guest
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
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
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
1
This
echo -n $VAR | wc -c
give27
, so newline character is thereâ George Udosen
54 mins ago
1
@GeorgeUdosen, probably this should be the answer :)
â pa4080
49 mins ago