How can I remove a newline from redirected output
Clash Royale CLAN TAG#URR8PPP
Can I remove a newline if I redirect output like this?
echo "a" >> file
I have something similar in my script which contains a for loop that redirects every character (except numbers) to a file.
But my goal is to have this output in one line. Let's say I redirect the "a" ten times so it should look like this:
a a a a a a a a a a
but it looks like this:
a
a
a
a
[etc.]
io-redirection
add a comment |
Can I remove a newline if I redirect output like this?
echo "a" >> file
I have something similar in my script which contains a for loop that redirects every character (except numbers) to a file.
But my goal is to have this output in one line. Let's say I redirect the "a" ten times so it should look like this:
a a a a a a a a a a
but it looks like this:
a
a
a
a
[etc.]
io-redirection
1
Useecho -n
orprintf
.
– DisplayName
Jan 2 at 19:35
I should have looked at man echo in the first place. Thanks a lot!
– KKor
Jan 2 at 19:38
3
@KKor don't rely onman echo
- your shell may have a builtinecho
command that behaves differently. See for example Why is printf better than echo?
– steeldriver
Jan 2 at 19:47
add a comment |
Can I remove a newline if I redirect output like this?
echo "a" >> file
I have something similar in my script which contains a for loop that redirects every character (except numbers) to a file.
But my goal is to have this output in one line. Let's say I redirect the "a" ten times so it should look like this:
a a a a a a a a a a
but it looks like this:
a
a
a
a
[etc.]
io-redirection
Can I remove a newline if I redirect output like this?
echo "a" >> file
I have something similar in my script which contains a for loop that redirects every character (except numbers) to a file.
But my goal is to have this output in one line. Let's say I redirect the "a" ten times so it should look like this:
a a a a a a a a a a
but it looks like this:
a
a
a
a
[etc.]
io-redirection
io-redirection
edited Jan 2 at 19:54
DopeGhoti
43.9k55382
43.9k55382
asked Jan 2 at 19:33
KKorKKor
154
154
1
Useecho -n
orprintf
.
– DisplayName
Jan 2 at 19:35
I should have looked at man echo in the first place. Thanks a lot!
– KKor
Jan 2 at 19:38
3
@KKor don't rely onman echo
- your shell may have a builtinecho
command that behaves differently. See for example Why is printf better than echo?
– steeldriver
Jan 2 at 19:47
add a comment |
1
Useecho -n
orprintf
.
– DisplayName
Jan 2 at 19:35
I should have looked at man echo in the first place. Thanks a lot!
– KKor
Jan 2 at 19:38
3
@KKor don't rely onman echo
- your shell may have a builtinecho
command that behaves differently. See for example Why is printf better than echo?
– steeldriver
Jan 2 at 19:47
1
1
Use
echo -n
or printf
.– DisplayName
Jan 2 at 19:35
Use
echo -n
or printf
.– DisplayName
Jan 2 at 19:35
I should have looked at man echo in the first place. Thanks a lot!
– KKor
Jan 2 at 19:38
I should have looked at man echo in the first place. Thanks a lot!
– KKor
Jan 2 at 19:38
3
3
@KKor don't rely on
man echo
- your shell may have a builtin echo
command that behaves differently. See for example Why is printf better than echo?– steeldriver
Jan 2 at 19:47
@KKor don't rely on
man echo
- your shell may have a builtin echo
command that behaves differently. See for example Why is printf better than echo?– steeldriver
Jan 2 at 19:47
add a comment |
3 Answers
3
active
oldest
votes
If you're specifically asking about echo
, you can use -n
to suppress the newline:
$ echo -n "test" >> outputfile
If you're asking more generally how to suppress newlines in piped or redirected data, this is a cat with many skins. One easy way is with tr
:
$ do_stuff | tr -d "n" >> outputfile
If you're just getting started with scripting and outputting data, it might be best to get out of the habit of using echo
altogether in favor of printf
:
$ printf "test" >> outputfile
printf
is superior to echo
in many ways and for many reasons, not least of which is that it only prints what you explicitly tell it to (and, arguably more importantly, in the format in which you want it).
add a comment |
I don't think you'd want to delete the newlines, but replace them with spaces:
for ((i=0; i<10; ++i)); do
echo a
done | tr 'n' ' ' >file
This simply post-processes your echo
output and saves the data into file
(truncating it first, if it already exists).
Or do something more fancy (in bash
),
for ((i=0; i<10; ++i)); do
array+=( "a" )
done
printf '%sn' "$array[*]" >file
This has the advantage of terminating the line properly with a final newline, while all the elements of the array are delimited by space (the first character of $IFS
).
Or, with /bin/sh
:
i=0
set --
while [ "$i" -lt 10 ]; do
set -- "$@" "a"
i=$(( i + 1 ))
done
printf '%sn' "$*" >file
add a comment |
for i in `seq 1 10`; do
printf "a " >> file
done
should do the trick.
1
orfor ... do ... done >> file
to avoid reopeningfile
multiple times
– Jeff Schaller
Jan 2 at 20:30
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%2f492071%2fhow-can-i-remove-a-newline-from-redirected-output%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
If you're specifically asking about echo
, you can use -n
to suppress the newline:
$ echo -n "test" >> outputfile
If you're asking more generally how to suppress newlines in piped or redirected data, this is a cat with many skins. One easy way is with tr
:
$ do_stuff | tr -d "n" >> outputfile
If you're just getting started with scripting and outputting data, it might be best to get out of the habit of using echo
altogether in favor of printf
:
$ printf "test" >> outputfile
printf
is superior to echo
in many ways and for many reasons, not least of which is that it only prints what you explicitly tell it to (and, arguably more importantly, in the format in which you want it).
add a comment |
If you're specifically asking about echo
, you can use -n
to suppress the newline:
$ echo -n "test" >> outputfile
If you're asking more generally how to suppress newlines in piped or redirected data, this is a cat with many skins. One easy way is with tr
:
$ do_stuff | tr -d "n" >> outputfile
If you're just getting started with scripting and outputting data, it might be best to get out of the habit of using echo
altogether in favor of printf
:
$ printf "test" >> outputfile
printf
is superior to echo
in many ways and for many reasons, not least of which is that it only prints what you explicitly tell it to (and, arguably more importantly, in the format in which you want it).
add a comment |
If you're specifically asking about echo
, you can use -n
to suppress the newline:
$ echo -n "test" >> outputfile
If you're asking more generally how to suppress newlines in piped or redirected data, this is a cat with many skins. One easy way is with tr
:
$ do_stuff | tr -d "n" >> outputfile
If you're just getting started with scripting and outputting data, it might be best to get out of the habit of using echo
altogether in favor of printf
:
$ printf "test" >> outputfile
printf
is superior to echo
in many ways and for many reasons, not least of which is that it only prints what you explicitly tell it to (and, arguably more importantly, in the format in which you want it).
If you're specifically asking about echo
, you can use -n
to suppress the newline:
$ echo -n "test" >> outputfile
If you're asking more generally how to suppress newlines in piped or redirected data, this is a cat with many skins. One easy way is with tr
:
$ do_stuff | tr -d "n" >> outputfile
If you're just getting started with scripting and outputting data, it might be best to get out of the habit of using echo
altogether in favor of printf
:
$ printf "test" >> outputfile
printf
is superior to echo
in many ways and for many reasons, not least of which is that it only prints what you explicitly tell it to (and, arguably more importantly, in the format in which you want it).
answered Jan 2 at 19:37
DopeGhotiDopeGhoti
43.9k55382
43.9k55382
add a comment |
add a comment |
I don't think you'd want to delete the newlines, but replace them with spaces:
for ((i=0; i<10; ++i)); do
echo a
done | tr 'n' ' ' >file
This simply post-processes your echo
output and saves the data into file
(truncating it first, if it already exists).
Or do something more fancy (in bash
),
for ((i=0; i<10; ++i)); do
array+=( "a" )
done
printf '%sn' "$array[*]" >file
This has the advantage of terminating the line properly with a final newline, while all the elements of the array are delimited by space (the first character of $IFS
).
Or, with /bin/sh
:
i=0
set --
while [ "$i" -lt 10 ]; do
set -- "$@" "a"
i=$(( i + 1 ))
done
printf '%sn' "$*" >file
add a comment |
I don't think you'd want to delete the newlines, but replace them with spaces:
for ((i=0; i<10; ++i)); do
echo a
done | tr 'n' ' ' >file
This simply post-processes your echo
output and saves the data into file
(truncating it first, if it already exists).
Or do something more fancy (in bash
),
for ((i=0; i<10; ++i)); do
array+=( "a" )
done
printf '%sn' "$array[*]" >file
This has the advantage of terminating the line properly with a final newline, while all the elements of the array are delimited by space (the first character of $IFS
).
Or, with /bin/sh
:
i=0
set --
while [ "$i" -lt 10 ]; do
set -- "$@" "a"
i=$(( i + 1 ))
done
printf '%sn' "$*" >file
add a comment |
I don't think you'd want to delete the newlines, but replace them with spaces:
for ((i=0; i<10; ++i)); do
echo a
done | tr 'n' ' ' >file
This simply post-processes your echo
output and saves the data into file
(truncating it first, if it already exists).
Or do something more fancy (in bash
),
for ((i=0; i<10; ++i)); do
array+=( "a" )
done
printf '%sn' "$array[*]" >file
This has the advantage of terminating the line properly with a final newline, while all the elements of the array are delimited by space (the first character of $IFS
).
Or, with /bin/sh
:
i=0
set --
while [ "$i" -lt 10 ]; do
set -- "$@" "a"
i=$(( i + 1 ))
done
printf '%sn' "$*" >file
I don't think you'd want to delete the newlines, but replace them with spaces:
for ((i=0; i<10; ++i)); do
echo a
done | tr 'n' ' ' >file
This simply post-processes your echo
output and saves the data into file
(truncating it first, if it already exists).
Or do something more fancy (in bash
),
for ((i=0; i<10; ++i)); do
array+=( "a" )
done
printf '%sn' "$array[*]" >file
This has the advantage of terminating the line properly with a final newline, while all the elements of the array are delimited by space (the first character of $IFS
).
Or, with /bin/sh
:
i=0
set --
while [ "$i" -lt 10 ]; do
set -- "$@" "a"
i=$(( i + 1 ))
done
printf '%sn' "$*" >file
answered Jan 2 at 20:22
KusalanandaKusalananda
124k16234385
124k16234385
add a comment |
add a comment |
for i in `seq 1 10`; do
printf "a " >> file
done
should do the trick.
1
orfor ... do ... done >> file
to avoid reopeningfile
multiple times
– Jeff Schaller
Jan 2 at 20:30
add a comment |
for i in `seq 1 10`; do
printf "a " >> file
done
should do the trick.
1
orfor ... do ... done >> file
to avoid reopeningfile
multiple times
– Jeff Schaller
Jan 2 at 20:30
add a comment |
for i in `seq 1 10`; do
printf "a " >> file
done
should do the trick.
for i in `seq 1 10`; do
printf "a " >> file
done
should do the trick.
edited Jan 2 at 20:30
Jeff Schaller
39.3k1054125
39.3k1054125
answered Jan 2 at 19:38
onur güngöronur güngör
764513
764513
1
orfor ... do ... done >> file
to avoid reopeningfile
multiple times
– Jeff Schaller
Jan 2 at 20:30
add a comment |
1
orfor ... do ... done >> file
to avoid reopeningfile
multiple times
– Jeff Schaller
Jan 2 at 20:30
1
1
or
for ... do ... done >> file
to avoid reopening file
multiple times– Jeff Schaller
Jan 2 at 20:30
or
for ... do ... done >> file
to avoid reopening file
multiple times– Jeff Schaller
Jan 2 at 20:30
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%2f492071%2fhow-can-i-remove-a-newline-from-redirected-output%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
1
Use
echo -n
orprintf
.– DisplayName
Jan 2 at 19:35
I should have looked at man echo in the first place. Thanks a lot!
– KKor
Jan 2 at 19:38
3
@KKor don't rely on
man echo
- your shell may have a builtinecho
command that behaves differently. See for example Why is printf better than echo?– steeldriver
Jan 2 at 19:47