Questions tagged [io-redirection]

I/O redirection is used in shell scripting. This feature enables one to take the output of a command and redirect it as an input for another command, or a command can take the input from a file instead of from the standard input stream or from the terminal. The whole process helps connect data streams in various ways.

Filter by
Sorted by
Tagged with
0 votes
1 answer
22 views

How to redirect while ignoring some of the text

Some potential duplicates may be the following: How to ignore specific lines from being redirected BUT I can't extrapolate from that to solve my problem. So, I am asking here instead. So, here is what ...
user avatar
4 votes
1 answer
171 views

Why I need to type press Enter key to finish this command? [duplicate]

I'm a new Linux user and I was doing some experiments and trying to understand Process Substitution. I believe I already have a basic understanding of it. But here is a case that I don't know why. I'm ...
user avatar
0 votes
1 answer
39 views

How to keep the output order when redirecting both stdout and stderr?

To describe the issue, I created a simple C program. root@u2004:~# cat test.c #include <stdio.h> int main(){ printf("output 1\n"); fprintf(stderr, "error 1\n"); ...
user avatar
0 votes
1 answer
23 views

stdin/stderr redirection, piping and color

I've seen a zillion permutations on this question but I think none is an exact duplicate. Can I copy only stderr (or eventually stderr and stdin) to a file, while keeping the colorized output on the ...
user avatar
1 vote
1 answer
30 views

How does Input/Output Redirection work in Linux/Unix?

Example: cat < test.txt Is the content of the file test.txt written/passed to stdin of the cat, and then the cat reads its stdin? OR Does the file test.txt itself become the stdin of the cat? ...
user avatar
0 votes
0 answers
24 views

pass output as grep pattern [duplicate]

I wish to pass each line of a file as a grep pattern to be searched in data.txt file. Thus, i need cat allsearchstrings.txt | grep <should be lines of allsearchstrings.txt> data.txt cat ...
user avatar
  • 297
0 votes
1 answer
37 views

What actually happens when I enter the following prompt on the CLI: cat demo.txt >> cat sample.txt?

Let say we have a text file demo.txt that only contains the word 'demo' and a text file sample.txt that only contains the word 'sample'. I understand that cat demo.txt >> sample.txt would append ...
user avatar
2 votes
0 answers
16 views

bash: debug (xtrace) target of redirections

Is there a way to show redirections with bash -x? $ bash -exc 'for word in foo bat baz; do echo $word >$word; done' + for word in foo bat baz + echo foo + for word in foo bat baz + echo bat + for ...
user avatar
  • 21
0 votes
1 answer
67 views

Can redirecting stderr and stdout to one file result in an error?

I'm writing a script that takes files (filename...) as arguments, and redirects both the stdout and stderr to a file of name filename.output. In the files are commands, and I'd only like to bash once (...
user avatar
  • 3
0 votes
0 answers
29 views

"> $logfile" does not truncate, file size goes to 0 and a second later, is back to full size [duplicate]

I have a script that writes to a logfile like this: $ nohup myscript.sh > myscript.out 2>&1 & when the log file gets very large, I need to truncate it like this: > myscript.out I ...
user avatar
  • 1
0 votes
2 answers
24 views

ps aux --headers problem on openSUSE Leap 15.3 when redirect output

The following works ok on openSUSE Leap 15.3. As in the header line is repeated for each screen of output when I scroll the screen back with the scroll bar on the terminal: ps aux --headers But when ...
user avatar
1 vote
0 answers
26 views

Why does redirection cause output to be duplicated? [duplicate]

In zsh, why does echo hi >&2 | cat produce the output hi hi ? My understanding is >&2 causes dup2(STDERR_FILENO, STDOUT_FILENO) to be called. | cat causes dup2(fd, STDOUT_FILENO) to ...
user avatar
1 vote
1 answer
33 views

Redirect Output Back to File [duplicate]

I issued the following command, thinking it would take the contents of foo.txt, process it, and write it back to the original file. Instead when I opened the file, it was empty. cat foo.txt | tr "...
user avatar
  • 115
0 votes
2 answers
41 views

Why do these two commands to write text-processing results back to the input file behave so differently?

I have a file authorized_keys and want to deduplicate the content, i.e. remove duplicate entries. I found two possible solutions to achieve this: Use cat and uniq, then redirect the output to the ...
user avatar
  • 1
0 votes
0 answers
26 views

How do I capture binary output from a process different from the console output?

I'm currently able to launch a process from within Java's ProcessBuilder, which continually appends to a (binary) file output.WAV, and is continually dumping lines of console updates. That works ...
user avatar
0 votes
2 answers
29 views

Redirect bash script output of application to file keeping the application interactive

I'm running a bash script expecting occasional user input to continue processing the script. Is it possible to redirect the script output to a file while keeping the terminal output and keeping the ...
user avatar
  • 269
0 votes
0 answers
22 views

File I/O redirection help in Bash for running badblocks into gzip and monitor with zcat [duplicate]

I am running this on a 10 TB bad drive, so the log grows out of proportions in an hour or so. This was my idea, not working until I kill the badblocks: badblocks -v -b 4096 -c 98304 -w -s /dev/disk/by-...
user avatar
2 votes
0 answers
56 views

What is the purpose of a pipe after stdout redirect?

I was reading the documentation for Kaniko and saw a shell redirect-then-pipe construct that I haven't seen before: echo -e 'FROM alpine \nRUN echo "created from standard input"' > ...
user avatar
0 votes
1 answer
80 views

grep screen output differs from output to file

When using grep (and sed also) the results I get in the terminal are different than what I get if I direct the output to file. My starting file is the captured output from a darknet yolo mAP run and ...
user avatar
0 votes
1 answer
118 views

echo "Hello World" > /dev/stdout prints nothing

Scenario: $ echo "Hello World" > /dev/stderr Hello World $ echo "Hello World" > /dev/stdout $ uname -a CYGWIN_NT-10.0 xxx 3.3.4(0.341/5/3) 2022-01-31 19:35 x86_64 Cygwin ...
user avatar
  • 355
2 votes
1 answer
59 views

POSIX Shell: Output to log and prepend lines with timestamps using a function

I would like a shell script to have all it's output for stdout and stderr logged to an external file with timestamps prepended to each line (plus a few extra items), without changing how the script is ...
user avatar
  • 73
0 votes
1 answer
89 views

CRON 2>&1 >/dev/null vs >/dev/null 2>&1 [duplicate]

I just created a tiny shell script to be run every minute. What happened was that it started flooding my mail. $ crontab -l * * * * * /home/user/bin/test 2>&1 >/dev/null I changed the ...
user avatar
0 votes
0 answers
23 views

redirect TTY input to output without serial cable

I'm using simulation software for unix (ucsim) that runs two simulated programs that supposed to connect to each other via its own simulated serial ports. The software provides me with these methods ...
user avatar
  • 1
1 vote
0 answers
38 views

SQL output piped to `kubectl exec` crashes, if not replacing TABS with SPACES

The context: I'm generating SQL from a backup dump, inside a subshell, to then pipe it to mysql for execution: ( echo "SET FOREIGN_KEY_CHECKS=0;"; tar --to-stdout -xvjf archive.tar; echo &...
user avatar
0 votes
1 answer
28 views

How might I save the the output of "find .... -execdir ..." to a log?

I cannot get the following script: #!/usr/bin/bash /usr/bin/find ~/CS -iname "*.html.pmd" -type f -execdir /usr/bin/raco pollen render {} \; > ~/rendering to redirect the output to the ...
user avatar
0 votes
0 answers
34 views

Why doesn't creating file with echo work for remote directories in this script?

I aliased the following script: #!/usr/bin/bash if [[ -d "template.html" ]]; then : else cp -n /home/$(whoami)/CS/Software/Pollen/template.html . fi /usr/bin/echo "#lang pollen [...
user avatar
0 votes
0 answers
75 views

Listening to stdout of a remote machine

I'm looking for a way to get a local terminal tail log from an stdout execution on a remote machine without directly executing with ssh. The general procedure is: The local client machine runs a ...
user avatar
  • 101
2 votes
1 answer
72 views

Create parent directories when using shell redirect

In Zsh, the following fails: $ echo hi > /tmp/this/path/does/not/exist/out.txt zsh: no such file or directory: /tmp/this/path/does/not/exist/out.txt Obviously the problem is that > cannot ...
user avatar
  • 674
1 vote
1 answer
73 views

Redirect stdout from a runing script

In C, you can redirect stdout somewhere when a program is running, for example: int fd = open("some_file", O_RDWR); dup2(fd, STDOUT_FILENO); printf("write to some_file\n"); Can I ...
user avatar
  • 205
3 votes
2 answers
259 views

How can I pipe a command to grep and get the exit status while still sending command output to stdout and errors to stderr?

Concrete use case: I'm trying to curl a URL with -v, output the verbose debug information to stderr as normal, output the response body to stdout as normal, but also check for the presence of a regexp ...
user avatar
1 vote
1 answer
56 views

Piping output from GitHub clone using tee misses output

I thought I would log the output of cloning a GitHub repo to a file. If I run git clone git@github.com:my_repo/my_repo.git | tee -a log I get an empty log file, but the following output to the ...
user avatar
8 votes
4 answers
2k views

How does "cat << EOF | grep" work in bash?

In this answer to How does "cat << EOF" work in bash? on Stack Overflow, I get the first two points. But I don't get the third point Pass multi-line string to a pipe in Bash Pass ...
user avatar
  • 411
1 vote
2 answers
96 views

Avoid creating an empty file when using redirection [duplicate]

foo | bar > out.txt would normally save the output to out.txt. However, if foo fails, then clearly bar cannot have any output, so it makes no sense to create out.txt. However, usually the result is ...
user avatar
  • 674
0 votes
1 answer
39 views

What's the "printing difference" between `git tag` and `git describe --tags`?

git tag seems to print in a separate process (window? app?) whereas git describe --tags is printed "in place". I can achieve the same printing behavior with git tag as git describe --tags by ...
user avatar
0 votes
0 answers
85 views

How to pipe an input into the cat command

I was reading an answer regarding sending large files from one Linux system to another by quick methods, and I was trying this solution The answer is saying that if you don't want to use the tar ...
user avatar
  • 159
0 votes
3 answers
76 views

Using `1>&- 2>&- &' as an alias after a shell command

To run a program with bash in background, with no text output, it is possible to use: 1>&- 2>&- &. I would like to define an alias for this. However, if I write in .bashrc alias ...
user avatar
  • 4,473
1 vote
1 answer
44 views

Temporarily showing stdout while a command is running

I'm scp'ing several large files to a few servers in succession. I'd like to see the progress (so I don't want to use scp --quiet), but I don't need to see the stats when the copy is complete. Is ...
user avatar
  • 2,279
0 votes
1 answer
81 views

How might I direct the output of "wget -qO- whatever.tar.gz" to both tar and sha256sum?

The following creates csfile with a hash in it, unpacks go1.17.4.linux-amd64.tar.gz downloaded from Golang's official website, prints out the checksum to stdout and then removes the no longer needed ...
user avatar
2 votes
0 answers
87 views

Possible reasons for redirection to /dev/null suddenly breaking

I am using a Bash loop to monitor a process that I spawned, and after some time (hours/days) it sometimes produces an "operation not permitted" error trying to redirect output to /dev/null: /...
user avatar
  • 43
0 votes
1 answer
60 views

Pipe output of two commands into another script as arguments

ntopng can handle multiple network interfaces and I've a script that takes the name of the interface and outputs it in to stdout. I can pipe this output into ntopng my_packet_tracer.sh | ntopng -i - ...
user avatar
0 votes
2 answers
56 views

exec vscode not redirecting output to /dev/null

I have the following bash script: exec '/mnt/c/Users/WindowsUserName/AppData/Local/Programs/Microsoft VS Code/Code.exe' $@ &>/dev/null & When I run the script I get ➜ ~ ./code foo.js ➜ ~ ...
user avatar
1 vote
1 answer
90 views

How to store '>&2' in a variable

Similar question, but no answer: How can we run a command stored in a variable? How to do the following in bash script? error=">&2" echo 'something went wrong' $error instead of echo ...
user avatar
  • 15
0 votes
1 answer
87 views

Why isn't "Permission denied" in stderr? [duplicate]

I have a command: mywritercmd -f /tmp/test 2>&1 > log.txt I run in a bash terminal, the command returns error: could not open output file "/tmp/test": Permission denied ...
user avatar
  • 432
0 votes
1 answer
40 views

redirect from right to left into code block

Consider this scenario: diff file.txt <( cat file.txt | grep -v '^\s*#'| sed '/^$/d' ) this is an example of redirect from right to left, in which you process a file and redirect the result as ...
user avatar
0 votes
2 answers
460 views

Why a Bash script still outputs to stdout even I redirect it to stderr?

I have a Bash script: #!/bin/bash echo this should be visible only in the stderr output but it is not 1>&2 exit 0 Also, I have a custom app that starts this script. The app captures both - ...
user avatar
  • 131
0 votes
0 answers
72 views

Diff between command > and command 1> ,>> [duplicate]

I am little bit confused about redirections in linux. command > file command 1> file command >> file.out 2>> file.err (this is working as expected) commmand 1>>file.out 2&...
user avatar
2 votes
1 answer
123 views

zsh: understanding sequence of redirections and pipe with pointers

In zsh, echo "hello" 1>&1 1>&1 1>&1 | cat prints hello 8 times, while echo "hello" 1>&1 1>&1 1>&1 1>&1 | cat prints hello 16 ...
user avatar
0 votes
0 answers
18 views

Discard Output Of Process but Allow Tailing (Within A New Session) As Needed

I have many processes that I run in the background that are redirected to /dev/null. I restart these processes in a new session in the event I need to see their output. I'd like to be able to tail ...
user avatar
  • 204
0 votes
1 answer
172 views

Closing stdout fd gives error, while closing stderr fd does not

The following test code gives error "bad file descriptor". Why is that so? This is just a test code to understand file descriptors and their interaction with pipes. Example 1) ❯ echo "...
user avatar
1 vote
1 answer
145 views

Redirection to stderr works in bash, not in zsh

In bash, ❯ echo "hello" 1>&2 | echo "world" hello world In zsh, ❯ echo "hello" 1>&2 | echo "world" world More than a way around this, I am ...
user avatar

1
2 3 4 5
28