Ignore spacing between echo strings
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I use echo
to print two strings into a file in either of these two commands:
echo -e "n""x" >> ~/file
echo -e "n" "x" >> ~/file
The first result is:
x
The second result is:
x
I assume you didn't miss the extra space.
I most desire that the second command would behave like the first one because the extra space between "n"
and "x"
makes it more comfortable to read the command.
Given it's not possible naturally in Bash 4.3.48(1), is there any syntax that will allow it or at least, come close to it?
The main goal is to have a spacing, or at least some placeholder/separator, that will give me a some kind of separation between the two strings, for more comfortable reading, but without having that extra space above.
Please show any way you know. I'm open to use something else than echo
(though I'd prefer to avoid here-string).
Note, I used the term "string" for "n
". I'm not sure it's correct here, but it's the best term I have to describe that.
text-processing io-redirection string echo whitespace
 |Â
show 1 more comment
up vote
2
down vote
favorite
I use echo
to print two strings into a file in either of these two commands:
echo -e "n""x" >> ~/file
echo -e "n" "x" >> ~/file
The first result is:
x
The second result is:
x
I assume you didn't miss the extra space.
I most desire that the second command would behave like the first one because the extra space between "n"
and "x"
makes it more comfortable to read the command.
Given it's not possible naturally in Bash 4.3.48(1), is there any syntax that will allow it or at least, come close to it?
The main goal is to have a spacing, or at least some placeholder/separator, that will give me a some kind of separation between the two strings, for more comfortable reading, but without having that extra space above.
Please show any way you know. I'm open to use something else than echo
(though I'd prefer to avoid here-string).
Note, I used the term "string" for "n
". I'm not sure it's correct here, but it's the best term I have to describe that.
text-processing io-redirection string echo whitespace
2
why notecho -e "n x"
?
â RomanPerekhrest
Feb 7 at 12:29
1
Possible duplicate of Why does my shell script choke on whitespace or other special characters?
â Jesse_b
Feb 7 at 12:32
1
While I'm sure there is a way to do what you want (There is almost always a way), to be clear: You are asking for a way to bypass the rules of the shell in order to make it easier for you to read? What about the people that come after you and read it?
â Jesse_b
Feb 7 at 12:39
1
@RomanPerekhrest it makes a single space beforex
in the second line... I don't want that single space beforex
. I want space only in the command itself for ease of read.
â user9303970
Feb 7 at 13:06
2
The same result could be had withecho; echo x; >file
.
â Kusalananda
Feb 7 at 13:17
 |Â
show 1 more comment
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I use echo
to print two strings into a file in either of these two commands:
echo -e "n""x" >> ~/file
echo -e "n" "x" >> ~/file
The first result is:
x
The second result is:
x
I assume you didn't miss the extra space.
I most desire that the second command would behave like the first one because the extra space between "n"
and "x"
makes it more comfortable to read the command.
Given it's not possible naturally in Bash 4.3.48(1), is there any syntax that will allow it or at least, come close to it?
The main goal is to have a spacing, or at least some placeholder/separator, that will give me a some kind of separation between the two strings, for more comfortable reading, but without having that extra space above.
Please show any way you know. I'm open to use something else than echo
(though I'd prefer to avoid here-string).
Note, I used the term "string" for "n
". I'm not sure it's correct here, but it's the best term I have to describe that.
text-processing io-redirection string echo whitespace
I use echo
to print two strings into a file in either of these two commands:
echo -e "n""x" >> ~/file
echo -e "n" "x" >> ~/file
The first result is:
x
The second result is:
x
I assume you didn't miss the extra space.
I most desire that the second command would behave like the first one because the extra space between "n"
and "x"
makes it more comfortable to read the command.
Given it's not possible naturally in Bash 4.3.48(1), is there any syntax that will allow it or at least, come close to it?
The main goal is to have a spacing, or at least some placeholder/separator, that will give me a some kind of separation between the two strings, for more comfortable reading, but without having that extra space above.
Please show any way you know. I'm open to use something else than echo
(though I'd prefer to avoid here-string).
Note, I used the term "string" for "n
". I'm not sure it's correct here, but it's the best term I have to describe that.
text-processing io-redirection string echo whitespace
asked Feb 7 at 12:26
user9303970
123224
123224
2
why notecho -e "n x"
?
â RomanPerekhrest
Feb 7 at 12:29
1
Possible duplicate of Why does my shell script choke on whitespace or other special characters?
â Jesse_b
Feb 7 at 12:32
1
While I'm sure there is a way to do what you want (There is almost always a way), to be clear: You are asking for a way to bypass the rules of the shell in order to make it easier for you to read? What about the people that come after you and read it?
â Jesse_b
Feb 7 at 12:39
1
@RomanPerekhrest it makes a single space beforex
in the second line... I don't want that single space beforex
. I want space only in the command itself for ease of read.
â user9303970
Feb 7 at 13:06
2
The same result could be had withecho; echo x; >file
.
â Kusalananda
Feb 7 at 13:17
 |Â
show 1 more comment
2
why notecho -e "n x"
?
â RomanPerekhrest
Feb 7 at 12:29
1
Possible duplicate of Why does my shell script choke on whitespace or other special characters?
â Jesse_b
Feb 7 at 12:32
1
While I'm sure there is a way to do what you want (There is almost always a way), to be clear: You are asking for a way to bypass the rules of the shell in order to make it easier for you to read? What about the people that come after you and read it?
â Jesse_b
Feb 7 at 12:39
1
@RomanPerekhrest it makes a single space beforex
in the second line... I don't want that single space beforex
. I want space only in the command itself for ease of read.
â user9303970
Feb 7 at 13:06
2
The same result could be had withecho; echo x; >file
.
â Kusalananda
Feb 7 at 13:17
2
2
why not
echo -e "n x"
?â RomanPerekhrest
Feb 7 at 12:29
why not
echo -e "n x"
?â RomanPerekhrest
Feb 7 at 12:29
1
1
Possible duplicate of Why does my shell script choke on whitespace or other special characters?
â Jesse_b
Feb 7 at 12:32
Possible duplicate of Why does my shell script choke on whitespace or other special characters?
â Jesse_b
Feb 7 at 12:32
1
1
While I'm sure there is a way to do what you want (There is almost always a way), to be clear: You are asking for a way to bypass the rules of the shell in order to make it easier for you to read? What about the people that come after you and read it?
â Jesse_b
Feb 7 at 12:39
While I'm sure there is a way to do what you want (There is almost always a way), to be clear: You are asking for a way to bypass the rules of the shell in order to make it easier for you to read? What about the people that come after you and read it?
â Jesse_b
Feb 7 at 12:39
1
1
@RomanPerekhrest it makes a single space before
x
in the second line... I don't want that single space before x
. I want space only in the command itself for ease of read.â user9303970
Feb 7 at 13:06
@RomanPerekhrest it makes a single space before
x
in the second line... I don't want that single space before x
. I want space only in the command itself for ease of read.â user9303970
Feb 7 at 13:06
2
2
The same result could be had with
echo; echo x; >file
.â Kusalananda
Feb 7 at 13:17
The same result could be had with
echo; echo x; >file
.â Kusalananda
Feb 7 at 13:17
 |Â
show 1 more comment
4 Answers
4
active
oldest
votes
up vote
3
down vote
accepted
You can do this with the printf
command:
printf 'n%sn' 'x' >file
With echo
, I was able to accomplish this using tr
to delete whitespace:
echo -e 'n x' | tr -d ' ' >file
Can also be written as (but please don't):
echo -e 'n' 'x' | tr -d ' ' >file
Results:
$ cat file
x
I suppose it could also easily be argued that these will actually make your command harder to read.
Nice! Thanks. What's the%s
inprintf
? I didn't find inman printf
.
â user9303970
Feb 7 at 13:17
2
@user9303970 It's a placeholder for a string which will be inserted from the arguments after the format string (n%sn
).man printf
probably documents the C library functionprintf()
, but it's using the same format string placeholders asprintf
in the shell.%s
is for "a string" while%d
is for "a decimal integer", etc. One could also specify field widths, left or right alignments etc.
â Kusalananda
Feb 7 at 13:29
1
What Kusalananda said ^^. I'm not great withprintf
but the bash-hackers wiki onprintf
is a good reference for me anyway.
â Jesse_b
Feb 7 at 13:31
add a comment |Â
up vote
3
down vote
If the key point is visually separating the two strings (n
and x
, here) in the script source, I suppose you could put them in variables:
str1='n'
str2='x'
echo -e "$str1$str2" >> file
The parameter expansions are of course back-to-back, but the strings themselves aren't.
Or with printf
, but adding the tailing newline must be done separately:
printf %s $'n' 'x'; echo; >> file
%s
of course doesn't expand backslash-escapes from the argument string, but in Bash we can use the $'...'
ANSI-C quoting that does. echo
to get the final newline.
Or define a function to do what you want ("$*"
concatenates the arguments to the function, without delimiter since IFS
was cleared here):
output() local IFS=; echo -e "$*";
output "n" "x"
yes,printf "%s" arg arg ...
is the answer.
â glenn jackman
Feb 7 at 13:39
add a comment |Â
up vote
1
down vote
If the objective is specifically to write a newline and an x
(and another newline?) to a file,
without having n
and x
appear consecutively (i.e.,â¯nx
)
in your command
(and without including an undesired space in your output),
a very simple approach is
echo; echo "x"; >> file
(to write newline, x
, newline), or
echo; echo -n "x"; >> file
(to write newline and x
, but no newline at the end).ÃÂ
You can also use parentheses instead of braces for grouping:
(echo; echo "x") >> file
Note that, with braces, you must have whitespace
(space(s), tab(s) and/or newline(s)) after the opening ,
and you must have a ;
(and/or newline(s)) before the closing .ÃÂ
Or you can do two totally separate (not grouped) commands
that do redirection independently:
echo >> file
echo "x" >> file
Or use Jesse_bâÂÂs answer:
printf 'n%sn' 'x' >> file
(to write newline, x
, newline), or
printf 'n%s' 'x' >> file
(to write newline and x
, but no newline at the end).
But if the objective is more general âÂÂ
to write a bunch of strings (with no intervening spaces),
while separating them by spaces on the command line, consider
printf '%b' string â¦
where %b
is the conversion format to print a string
with sequences interpreted (in contrast to
%s
,
which prints strings literally).ÃÂ
For example,
printf '%b' 'n' foo 't' bar
will write newlinefoo
tabbar
;
i.e.,
Â
foo bar
(with no newline at the end).ÃÂ
You could also say
printf '%b%b%b%b' 'n' foo 't' bar
but you donâÂÂt need to;
the format will be applied as many times as necessary
to display all the arguments.ÃÂ
Therefore, if you want to add a newline at the end,
printf '%bn' 'n' foo 't' bar
will not work; it will add a newline after each argument.ÃÂ
You can do
printf '%b' 'n' foo 't' bar 'n'
(adding a newline as a final argument), or
printf '%b%b%b%bn' 'n' foo 't' bar
(counting the arguments), or
printf '%b' 'n' foo 't' bar; echo;
Yet another approach, if youâÂÂre using bash, is
printf '%s' $'n' foo $'t' bar
using bashâÂÂs $'textâ¦'
notation
to interpret sequences.ÃÂ
Applying this to the original question, we would get
printf '%s' $'n' x
Of course I had finished typing my answer before I saw this one.
â G-Man
Feb 7 at 17:51
Thumbed for the rich, didactic answer.
â user9303970
Feb 8 at 0:44
G-Man, as you suggested I checked the bytes of the files. Indeed there was a change of one byte. Without any of the two changes, at the start, the file had 2188 bytes. Withn%s
it had 2217. I then changed it ton%sn
and got 2218, so that extran%sn
is all the change. I thank you Jesse_b and G-Man for your help.
â user9303970
Feb 8 at 0:59
add a comment |Â
up vote
1
down vote
You can use the %b
(interpret backquoted arguments) of bash printf:
$ printf %b \n x >> file
Of course, you can quote the n
:
$ printf %b 'n' x >> file
But be very careful with what you feed to printf as any backquoted value instead of x
will be interpreted by printf.
Or, since you are using bash, it allows a newline to be generated with $'n'
:
$ printf '%s' $'n' x >>file
Somewhat safer than %b
.
Or, if you want a variable:
$ nl=$'n'
$ printf '%s' "$nl" x >> file
Or you can define a function:
$ separate() local IFS=; printf %b "$*";
$ separate 'n' 'x' '*'
The local IFS=
will ensure the arguments are joined together (no spaces in between) by "$*"
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
You can do this with the printf
command:
printf 'n%sn' 'x' >file
With echo
, I was able to accomplish this using tr
to delete whitespace:
echo -e 'n x' | tr -d ' ' >file
Can also be written as (but please don't):
echo -e 'n' 'x' | tr -d ' ' >file
Results:
$ cat file
x
I suppose it could also easily be argued that these will actually make your command harder to read.
Nice! Thanks. What's the%s
inprintf
? I didn't find inman printf
.
â user9303970
Feb 7 at 13:17
2
@user9303970 It's a placeholder for a string which will be inserted from the arguments after the format string (n%sn
).man printf
probably documents the C library functionprintf()
, but it's using the same format string placeholders asprintf
in the shell.%s
is for "a string" while%d
is for "a decimal integer", etc. One could also specify field widths, left or right alignments etc.
â Kusalananda
Feb 7 at 13:29
1
What Kusalananda said ^^. I'm not great withprintf
but the bash-hackers wiki onprintf
is a good reference for me anyway.
â Jesse_b
Feb 7 at 13:31
add a comment |Â
up vote
3
down vote
accepted
You can do this with the printf
command:
printf 'n%sn' 'x' >file
With echo
, I was able to accomplish this using tr
to delete whitespace:
echo -e 'n x' | tr -d ' ' >file
Can also be written as (but please don't):
echo -e 'n' 'x' | tr -d ' ' >file
Results:
$ cat file
x
I suppose it could also easily be argued that these will actually make your command harder to read.
Nice! Thanks. What's the%s
inprintf
? I didn't find inman printf
.
â user9303970
Feb 7 at 13:17
2
@user9303970 It's a placeholder for a string which will be inserted from the arguments after the format string (n%sn
).man printf
probably documents the C library functionprintf()
, but it's using the same format string placeholders asprintf
in the shell.%s
is for "a string" while%d
is for "a decimal integer", etc. One could also specify field widths, left or right alignments etc.
â Kusalananda
Feb 7 at 13:29
1
What Kusalananda said ^^. I'm not great withprintf
but the bash-hackers wiki onprintf
is a good reference for me anyway.
â Jesse_b
Feb 7 at 13:31
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
You can do this with the printf
command:
printf 'n%sn' 'x' >file
With echo
, I was able to accomplish this using tr
to delete whitespace:
echo -e 'n x' | tr -d ' ' >file
Can also be written as (but please don't):
echo -e 'n' 'x' | tr -d ' ' >file
Results:
$ cat file
x
I suppose it could also easily be argued that these will actually make your command harder to read.
You can do this with the printf
command:
printf 'n%sn' 'x' >file
With echo
, I was able to accomplish this using tr
to delete whitespace:
echo -e 'n x' | tr -d ' ' >file
Can also be written as (but please don't):
echo -e 'n' 'x' | tr -d ' ' >file
Results:
$ cat file
x
I suppose it could also easily be argued that these will actually make your command harder to read.
edited Feb 7 at 18:33
answered Feb 7 at 12:51
Jesse_b
10.4k22658
10.4k22658
Nice! Thanks. What's the%s
inprintf
? I didn't find inman printf
.
â user9303970
Feb 7 at 13:17
2
@user9303970 It's a placeholder for a string which will be inserted from the arguments after the format string (n%sn
).man printf
probably documents the C library functionprintf()
, but it's using the same format string placeholders asprintf
in the shell.%s
is for "a string" while%d
is for "a decimal integer", etc. One could also specify field widths, left or right alignments etc.
â Kusalananda
Feb 7 at 13:29
1
What Kusalananda said ^^. I'm not great withprintf
but the bash-hackers wiki onprintf
is a good reference for me anyway.
â Jesse_b
Feb 7 at 13:31
add a comment |Â
Nice! Thanks. What's the%s
inprintf
? I didn't find inman printf
.
â user9303970
Feb 7 at 13:17
2
@user9303970 It's a placeholder for a string which will be inserted from the arguments after the format string (n%sn
).man printf
probably documents the C library functionprintf()
, but it's using the same format string placeholders asprintf
in the shell.%s
is for "a string" while%d
is for "a decimal integer", etc. One could also specify field widths, left or right alignments etc.
â Kusalananda
Feb 7 at 13:29
1
What Kusalananda said ^^. I'm not great withprintf
but the bash-hackers wiki onprintf
is a good reference for me anyway.
â Jesse_b
Feb 7 at 13:31
Nice! Thanks. What's the
%s
in printf
? I didn't find in man printf
.â user9303970
Feb 7 at 13:17
Nice! Thanks. What's the
%s
in printf
? I didn't find in man printf
.â user9303970
Feb 7 at 13:17
2
2
@user9303970 It's a placeholder for a string which will be inserted from the arguments after the format string (
n%sn
). man printf
probably documents the C library function printf()
, but it's using the same format string placeholders as printf
in the shell. %s
is for "a string" while %d
is for "a decimal integer", etc. One could also specify field widths, left or right alignments etc.â Kusalananda
Feb 7 at 13:29
@user9303970 It's a placeholder for a string which will be inserted from the arguments after the format string (
n%sn
). man printf
probably documents the C library function printf()
, but it's using the same format string placeholders as printf
in the shell. %s
is for "a string" while %d
is for "a decimal integer", etc. One could also specify field widths, left or right alignments etc.â Kusalananda
Feb 7 at 13:29
1
1
What Kusalananda said ^^. I'm not great with
printf
but the bash-hackers wiki on printf
is a good reference for me anyway.â Jesse_b
Feb 7 at 13:31
What Kusalananda said ^^. I'm not great with
printf
but the bash-hackers wiki on printf
is a good reference for me anyway.â Jesse_b
Feb 7 at 13:31
add a comment |Â
up vote
3
down vote
If the key point is visually separating the two strings (n
and x
, here) in the script source, I suppose you could put them in variables:
str1='n'
str2='x'
echo -e "$str1$str2" >> file
The parameter expansions are of course back-to-back, but the strings themselves aren't.
Or with printf
, but adding the tailing newline must be done separately:
printf %s $'n' 'x'; echo; >> file
%s
of course doesn't expand backslash-escapes from the argument string, but in Bash we can use the $'...'
ANSI-C quoting that does. echo
to get the final newline.
Or define a function to do what you want ("$*"
concatenates the arguments to the function, without delimiter since IFS
was cleared here):
output() local IFS=; echo -e "$*";
output "n" "x"
yes,printf "%s" arg arg ...
is the answer.
â glenn jackman
Feb 7 at 13:39
add a comment |Â
up vote
3
down vote
If the key point is visually separating the two strings (n
and x
, here) in the script source, I suppose you could put them in variables:
str1='n'
str2='x'
echo -e "$str1$str2" >> file
The parameter expansions are of course back-to-back, but the strings themselves aren't.
Or with printf
, but adding the tailing newline must be done separately:
printf %s $'n' 'x'; echo; >> file
%s
of course doesn't expand backslash-escapes from the argument string, but in Bash we can use the $'...'
ANSI-C quoting that does. echo
to get the final newline.
Or define a function to do what you want ("$*"
concatenates the arguments to the function, without delimiter since IFS
was cleared here):
output() local IFS=; echo -e "$*";
output "n" "x"
yes,printf "%s" arg arg ...
is the answer.
â glenn jackman
Feb 7 at 13:39
add a comment |Â
up vote
3
down vote
up vote
3
down vote
If the key point is visually separating the two strings (n
and x
, here) in the script source, I suppose you could put them in variables:
str1='n'
str2='x'
echo -e "$str1$str2" >> file
The parameter expansions are of course back-to-back, but the strings themselves aren't.
Or with printf
, but adding the tailing newline must be done separately:
printf %s $'n' 'x'; echo; >> file
%s
of course doesn't expand backslash-escapes from the argument string, but in Bash we can use the $'...'
ANSI-C quoting that does. echo
to get the final newline.
Or define a function to do what you want ("$*"
concatenates the arguments to the function, without delimiter since IFS
was cleared here):
output() local IFS=; echo -e "$*";
output "n" "x"
If the key point is visually separating the two strings (n
and x
, here) in the script source, I suppose you could put them in variables:
str1='n'
str2='x'
echo -e "$str1$str2" >> file
The parameter expansions are of course back-to-back, but the strings themselves aren't.
Or with printf
, but adding the tailing newline must be done separately:
printf %s $'n' 'x'; echo; >> file
%s
of course doesn't expand backslash-escapes from the argument string, but in Bash we can use the $'...'
ANSI-C quoting that does. echo
to get the final newline.
Or define a function to do what you want ("$*"
concatenates the arguments to the function, without delimiter since IFS
was cleared here):
output() local IFS=; echo -e "$*";
output "n" "x"
edited Feb 7 at 13:52
answered Feb 7 at 13:31
ilkkachu
49.6k673137
49.6k673137
yes,printf "%s" arg arg ...
is the answer.
â glenn jackman
Feb 7 at 13:39
add a comment |Â
yes,printf "%s" arg arg ...
is the answer.
â glenn jackman
Feb 7 at 13:39
yes,
printf "%s" arg arg ...
is the answer.â glenn jackman
Feb 7 at 13:39
yes,
printf "%s" arg arg ...
is the answer.â glenn jackman
Feb 7 at 13:39
add a comment |Â
up vote
1
down vote
If the objective is specifically to write a newline and an x
(and another newline?) to a file,
without having n
and x
appear consecutively (i.e.,â¯nx
)
in your command
(and without including an undesired space in your output),
a very simple approach is
echo; echo "x"; >> file
(to write newline, x
, newline), or
echo; echo -n "x"; >> file
(to write newline and x
, but no newline at the end).ÃÂ
You can also use parentheses instead of braces for grouping:
(echo; echo "x") >> file
Note that, with braces, you must have whitespace
(space(s), tab(s) and/or newline(s)) after the opening ,
and you must have a ;
(and/or newline(s)) before the closing .ÃÂ
Or you can do two totally separate (not grouped) commands
that do redirection independently:
echo >> file
echo "x" >> file
Or use Jesse_bâÂÂs answer:
printf 'n%sn' 'x' >> file
(to write newline, x
, newline), or
printf 'n%s' 'x' >> file
(to write newline and x
, but no newline at the end).
But if the objective is more general âÂÂ
to write a bunch of strings (with no intervening spaces),
while separating them by spaces on the command line, consider
printf '%b' string â¦
where %b
is the conversion format to print a string
with sequences interpreted (in contrast to
%s
,
which prints strings literally).ÃÂ
For example,
printf '%b' 'n' foo 't' bar
will write newlinefoo
tabbar
;
i.e.,
Â
foo bar
(with no newline at the end).ÃÂ
You could also say
printf '%b%b%b%b' 'n' foo 't' bar
but you donâÂÂt need to;
the format will be applied as many times as necessary
to display all the arguments.ÃÂ
Therefore, if you want to add a newline at the end,
printf '%bn' 'n' foo 't' bar
will not work; it will add a newline after each argument.ÃÂ
You can do
printf '%b' 'n' foo 't' bar 'n'
(adding a newline as a final argument), or
printf '%b%b%b%bn' 'n' foo 't' bar
(counting the arguments), or
printf '%b' 'n' foo 't' bar; echo;
Yet another approach, if youâÂÂre using bash, is
printf '%s' $'n' foo $'t' bar
using bashâÂÂs $'textâ¦'
notation
to interpret sequences.ÃÂ
Applying this to the original question, we would get
printf '%s' $'n' x
Of course I had finished typing my answer before I saw this one.
â G-Man
Feb 7 at 17:51
Thumbed for the rich, didactic answer.
â user9303970
Feb 8 at 0:44
G-Man, as you suggested I checked the bytes of the files. Indeed there was a change of one byte. Without any of the two changes, at the start, the file had 2188 bytes. Withn%s
it had 2217. I then changed it ton%sn
and got 2218, so that extran%sn
is all the change. I thank you Jesse_b and G-Man for your help.
â user9303970
Feb 8 at 0:59
add a comment |Â
up vote
1
down vote
If the objective is specifically to write a newline and an x
(and another newline?) to a file,
without having n
and x
appear consecutively (i.e.,â¯nx
)
in your command
(and without including an undesired space in your output),
a very simple approach is
echo; echo "x"; >> file
(to write newline, x
, newline), or
echo; echo -n "x"; >> file
(to write newline and x
, but no newline at the end).ÃÂ
You can also use parentheses instead of braces for grouping:
(echo; echo "x") >> file
Note that, with braces, you must have whitespace
(space(s), tab(s) and/or newline(s)) after the opening ,
and you must have a ;
(and/or newline(s)) before the closing .ÃÂ
Or you can do two totally separate (not grouped) commands
that do redirection independently:
echo >> file
echo "x" >> file
Or use Jesse_bâÂÂs answer:
printf 'n%sn' 'x' >> file
(to write newline, x
, newline), or
printf 'n%s' 'x' >> file
(to write newline and x
, but no newline at the end).
But if the objective is more general âÂÂ
to write a bunch of strings (with no intervening spaces),
while separating them by spaces on the command line, consider
printf '%b' string â¦
where %b
is the conversion format to print a string
with sequences interpreted (in contrast to
%s
,
which prints strings literally).ÃÂ
For example,
printf '%b' 'n' foo 't' bar
will write newlinefoo
tabbar
;
i.e.,
Â
foo bar
(with no newline at the end).ÃÂ
You could also say
printf '%b%b%b%b' 'n' foo 't' bar
but you donâÂÂt need to;
the format will be applied as many times as necessary
to display all the arguments.ÃÂ
Therefore, if you want to add a newline at the end,
printf '%bn' 'n' foo 't' bar
will not work; it will add a newline after each argument.ÃÂ
You can do
printf '%b' 'n' foo 't' bar 'n'
(adding a newline as a final argument), or
printf '%b%b%b%bn' 'n' foo 't' bar
(counting the arguments), or
printf '%b' 'n' foo 't' bar; echo;
Yet another approach, if youâÂÂre using bash, is
printf '%s' $'n' foo $'t' bar
using bashâÂÂs $'textâ¦'
notation
to interpret sequences.ÃÂ
Applying this to the original question, we would get
printf '%s' $'n' x
Of course I had finished typing my answer before I saw this one.
â G-Man
Feb 7 at 17:51
Thumbed for the rich, didactic answer.
â user9303970
Feb 8 at 0:44
G-Man, as you suggested I checked the bytes of the files. Indeed there was a change of one byte. Without any of the two changes, at the start, the file had 2188 bytes. Withn%s
it had 2217. I then changed it ton%sn
and got 2218, so that extran%sn
is all the change. I thank you Jesse_b and G-Man for your help.
â user9303970
Feb 8 at 0:59
add a comment |Â
up vote
1
down vote
up vote
1
down vote
If the objective is specifically to write a newline and an x
(and another newline?) to a file,
without having n
and x
appear consecutively (i.e.,â¯nx
)
in your command
(and without including an undesired space in your output),
a very simple approach is
echo; echo "x"; >> file
(to write newline, x
, newline), or
echo; echo -n "x"; >> file
(to write newline and x
, but no newline at the end).ÃÂ
You can also use parentheses instead of braces for grouping:
(echo; echo "x") >> file
Note that, with braces, you must have whitespace
(space(s), tab(s) and/or newline(s)) after the opening ,
and you must have a ;
(and/or newline(s)) before the closing .ÃÂ
Or you can do two totally separate (not grouped) commands
that do redirection independently:
echo >> file
echo "x" >> file
Or use Jesse_bâÂÂs answer:
printf 'n%sn' 'x' >> file
(to write newline, x
, newline), or
printf 'n%s' 'x' >> file
(to write newline and x
, but no newline at the end).
But if the objective is more general âÂÂ
to write a bunch of strings (with no intervening spaces),
while separating them by spaces on the command line, consider
printf '%b' string â¦
where %b
is the conversion format to print a string
with sequences interpreted (in contrast to
%s
,
which prints strings literally).ÃÂ
For example,
printf '%b' 'n' foo 't' bar
will write newlinefoo
tabbar
;
i.e.,
Â
foo bar
(with no newline at the end).ÃÂ
You could also say
printf '%b%b%b%b' 'n' foo 't' bar
but you donâÂÂt need to;
the format will be applied as many times as necessary
to display all the arguments.ÃÂ
Therefore, if you want to add a newline at the end,
printf '%bn' 'n' foo 't' bar
will not work; it will add a newline after each argument.ÃÂ
You can do
printf '%b' 'n' foo 't' bar 'n'
(adding a newline as a final argument), or
printf '%b%b%b%bn' 'n' foo 't' bar
(counting the arguments), or
printf '%b' 'n' foo 't' bar; echo;
Yet another approach, if youâÂÂre using bash, is
printf '%s' $'n' foo $'t' bar
using bashâÂÂs $'textâ¦'
notation
to interpret sequences.ÃÂ
Applying this to the original question, we would get
printf '%s' $'n' x
If the objective is specifically to write a newline and an x
(and another newline?) to a file,
without having n
and x
appear consecutively (i.e.,â¯nx
)
in your command
(and without including an undesired space in your output),
a very simple approach is
echo; echo "x"; >> file
(to write newline, x
, newline), or
echo; echo -n "x"; >> file
(to write newline and x
, but no newline at the end).ÃÂ
You can also use parentheses instead of braces for grouping:
(echo; echo "x") >> file
Note that, with braces, you must have whitespace
(space(s), tab(s) and/or newline(s)) after the opening ,
and you must have a ;
(and/or newline(s)) before the closing .ÃÂ
Or you can do two totally separate (not grouped) commands
that do redirection independently:
echo >> file
echo "x" >> file
Or use Jesse_bâÂÂs answer:
printf 'n%sn' 'x' >> file
(to write newline, x
, newline), or
printf 'n%s' 'x' >> file
(to write newline and x
, but no newline at the end).
But if the objective is more general âÂÂ
to write a bunch of strings (with no intervening spaces),
while separating them by spaces on the command line, consider
printf '%b' string â¦
where %b
is the conversion format to print a string
with sequences interpreted (in contrast to
%s
,
which prints strings literally).ÃÂ
For example,
printf '%b' 'n' foo 't' bar
will write newlinefoo
tabbar
;
i.e.,
Â
foo bar
(with no newline at the end).ÃÂ
You could also say
printf '%b%b%b%b' 'n' foo 't' bar
but you donâÂÂt need to;
the format will be applied as many times as necessary
to display all the arguments.ÃÂ
Therefore, if you want to add a newline at the end,
printf '%bn' 'n' foo 't' bar
will not work; it will add a newline after each argument.ÃÂ
You can do
printf '%b' 'n' foo 't' bar 'n'
(adding a newline as a final argument), or
printf '%b%b%b%bn' 'n' foo 't' bar
(counting the arguments), or
printf '%b' 'n' foo 't' bar; echo;
Yet another approach, if youâÂÂre using bash, is
printf '%s' $'n' foo $'t' bar
using bashâÂÂs $'textâ¦'
notation
to interpret sequences.ÃÂ
Applying this to the original question, we would get
printf '%s' $'n' x
answered Feb 7 at 17:42
G-Man
11.5k82656
11.5k82656
Of course I had finished typing my answer before I saw this one.
â G-Man
Feb 7 at 17:51
Thumbed for the rich, didactic answer.
â user9303970
Feb 8 at 0:44
G-Man, as you suggested I checked the bytes of the files. Indeed there was a change of one byte. Without any of the two changes, at the start, the file had 2188 bytes. Withn%s
it had 2217. I then changed it ton%sn
and got 2218, so that extran%sn
is all the change. I thank you Jesse_b and G-Man for your help.
â user9303970
Feb 8 at 0:59
add a comment |Â
Of course I had finished typing my answer before I saw this one.
â G-Man
Feb 7 at 17:51
Thumbed for the rich, didactic answer.
â user9303970
Feb 8 at 0:44
G-Man, as you suggested I checked the bytes of the files. Indeed there was a change of one byte. Without any of the two changes, at the start, the file had 2188 bytes. Withn%s
it had 2217. I then changed it ton%sn
and got 2218, so that extran%sn
is all the change. I thank you Jesse_b and G-Man for your help.
â user9303970
Feb 8 at 0:59
Of course I had finished typing my answer before I saw this one.
â G-Man
Feb 7 at 17:51
Of course I had finished typing my answer before I saw this one.
â G-Man
Feb 7 at 17:51
Thumbed for the rich, didactic answer.
â user9303970
Feb 8 at 0:44
Thumbed for the rich, didactic answer.
â user9303970
Feb 8 at 0:44
G-Man, as you suggested I checked the bytes of the files. Indeed there was a change of one byte. Without any of the two changes, at the start, the file had 2188 bytes. With
n%s
it had 2217. I then changed it to n%sn
and got 2218, so that extra n%sn
is all the change. I thank you Jesse_b and G-Man for your help.â user9303970
Feb 8 at 0:59
G-Man, as you suggested I checked the bytes of the files. Indeed there was a change of one byte. Without any of the two changes, at the start, the file had 2188 bytes. With
n%s
it had 2217. I then changed it to n%sn
and got 2218, so that extra n%sn
is all the change. I thank you Jesse_b and G-Man for your help.â user9303970
Feb 8 at 0:59
add a comment |Â
up vote
1
down vote
You can use the %b
(interpret backquoted arguments) of bash printf:
$ printf %b \n x >> file
Of course, you can quote the n
:
$ printf %b 'n' x >> file
But be very careful with what you feed to printf as any backquoted value instead of x
will be interpreted by printf.
Or, since you are using bash, it allows a newline to be generated with $'n'
:
$ printf '%s' $'n' x >>file
Somewhat safer than %b
.
Or, if you want a variable:
$ nl=$'n'
$ printf '%s' "$nl" x >> file
Or you can define a function:
$ separate() local IFS=; printf %b "$*";
$ separate 'n' 'x' '*'
The local IFS=
will ensure the arguments are joined together (no spaces in between) by "$*"
add a comment |Â
up vote
1
down vote
You can use the %b
(interpret backquoted arguments) of bash printf:
$ printf %b \n x >> file
Of course, you can quote the n
:
$ printf %b 'n' x >> file
But be very careful with what you feed to printf as any backquoted value instead of x
will be interpreted by printf.
Or, since you are using bash, it allows a newline to be generated with $'n'
:
$ printf '%s' $'n' x >>file
Somewhat safer than %b
.
Or, if you want a variable:
$ nl=$'n'
$ printf '%s' "$nl" x >> file
Or you can define a function:
$ separate() local IFS=; printf %b "$*";
$ separate 'n' 'x' '*'
The local IFS=
will ensure the arguments are joined together (no spaces in between) by "$*"
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You can use the %b
(interpret backquoted arguments) of bash printf:
$ printf %b \n x >> file
Of course, you can quote the n
:
$ printf %b 'n' x >> file
But be very careful with what you feed to printf as any backquoted value instead of x
will be interpreted by printf.
Or, since you are using bash, it allows a newline to be generated with $'n'
:
$ printf '%s' $'n' x >>file
Somewhat safer than %b
.
Or, if you want a variable:
$ nl=$'n'
$ printf '%s' "$nl" x >> file
Or you can define a function:
$ separate() local IFS=; printf %b "$*";
$ separate 'n' 'x' '*'
The local IFS=
will ensure the arguments are joined together (no spaces in between) by "$*"
You can use the %b
(interpret backquoted arguments) of bash printf:
$ printf %b \n x >> file
Of course, you can quote the n
:
$ printf %b 'n' x >> file
But be very careful with what you feed to printf as any backquoted value instead of x
will be interpreted by printf.
Or, since you are using bash, it allows a newline to be generated with $'n'
:
$ printf '%s' $'n' x >>file
Somewhat safer than %b
.
Or, if you want a variable:
$ nl=$'n'
$ printf '%s' "$nl" x >> file
Or you can define a function:
$ separate() local IFS=; printf %b "$*";
$ separate 'n' 'x' '*'
The local IFS=
will ensure the arguments are joined together (no spaces in between) by "$*"
edited Feb 7 at 18:06
answered Feb 7 at 17:32
Isaac
6,6381734
6,6381734
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%2funix.stackexchange.com%2fquestions%2f422525%2fignore-spacing-between-echo-strings%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
2
why not
echo -e "n x"
?â RomanPerekhrest
Feb 7 at 12:29
1
Possible duplicate of Why does my shell script choke on whitespace or other special characters?
â Jesse_b
Feb 7 at 12:32
1
While I'm sure there is a way to do what you want (There is almost always a way), to be clear: You are asking for a way to bypass the rules of the shell in order to make it easier for you to read? What about the people that come after you and read it?
â Jesse_b
Feb 7 at 12:39
1
@RomanPerekhrest it makes a single space before
x
in the second line... I don't want that single space beforex
. I want space only in the command itself for ease of read.â user9303970
Feb 7 at 13:06
2
The same result could be had with
echo; echo x; >file
.â Kusalananda
Feb 7 at 13:17