In CentOS, I would like to run a script which is archived as a .tar file, but when I try to do it, it does not recognize 'read' command.

The main idea is run this script without save on disk

  • Listing the contents or the .tar file tar -tf script.rar
  • Returns it: teste-1.sh
[user@host]$ tar -tf script.rar 

Making untar,or extracting the file and trying to execute it

  • tar -xvf script.rar | xargs bash
  • Returns your full info is:
[user@host]$ tar -xvf script.rar | xargs bash 
your full info is:  
[user@host]$ tar -xvf script.rar | bash -
bash: line 1: teste-1.sh: command not found
[user@host]$ tar -xvf script.rar | sh -
sh: line 1: teste-1.sh: command not found

Like shown above, the output is the last line of the code and not shows the read prompt. The full simple code just for test purpose is:

read -p "type your name:" name
read -p "type lastname:" lastname
echo "your full info is: $name $lastname"

When I try to execute it manually , it works without problems (without being archived)

[user@host]$ ./teste-1.sh 
type your name:jhon
type lastname:doe
your full info is: jhon doe
  • I have tried to use | sh - , | bash - , but without success. Does anyone have an interesting idea?, thank you so much!
  • 2
    rar or tar?
    – Jasen
    Jun 28, 2021 at 6:08

1 Answer 1



cmd1 | xargs cmd2

xargs takes the output of cmd1, splits it into words (whitespace separated, understanding quotes in its very own way), and calls cmd2 with those words as arguments.

With tar -xvf somearchive, tar extracts the archive member on disk. Thanks to that -v, it does also produce some output, but depending on the implementation, that will go either on stdout or stderr and the format will also vary greatly between implementation. So xargs bash will either see nothing on its stdin, or a list of archive members in one format or another.

So xargs will call bash either without arguments (and then bash will interpret the empty script it is being fed on stdin¹ so do nothing).

With tar implementations that do output just the archive member with -v on stdout, that will call bash teste-1.sh (with a now empty stdin). Assuming tar doesn't close its stdout until it has finished extracting the archive, the teste-1.sh will have been fully extracted on disk by the time bash starts, so bash should be able to open and interpret the script.

Here, if you don't want the file to be extracted on disk, what you'd need is to pass the contents of the teste-1.sh member of the archive to bash on its stdin, not xargs for bash to interpret the code in it:

tar -xOf somearchive teste-1.sh | bash

(where -O as supported by a few tar implementations tells tar to dump the contents of the extracted members on its standard output instead of creating the files on disk).

We're passing the name of the archive member we want to extract as you likely don't want to pass more than one script for bash to interpret. Note that the name of the archive member must be passed exactly as listed by tar tf.

That assumes that test-1.sh script doesn't read its stdin. If it does, it will also read the contents of the script (of itself!) under bash's feet. In the other cases above, the script would read nothing as the input would be empty.

One way to avoid that would be to call it as:

bash <(tar -xOf somearchive test-1.sh)

Where this time, the contents of the script is passed to bash as if from a named pipe, so stdin is not affected.

¹ depending on the xargs implementation, in xargs cmd, cmd's input is either the same as xargs (so here the empty pipe), or /dev/null (also empty)

  • Hi @Stéphane Chazelas , thank you for your answer, I performed as you suggested but without success. [user@host]$ tar -xOf script.rar teste-1.sh | bash and it returned your full info is: read -p "type lastname:" lastname
    – dcubaz
    Jun 27, 2021 at 20:07
  • 1
    @dcubaz, see edit. tar being a non-standard command, the behaviour will vary greatly with the implementation as well. Jun 27, 2021 at 20:15
  • @StéphaneChazelas, I see, i though could be possible the script works normally running on the fly without save it. I tried the last command, but with not success.
    – dcubaz
    Jun 27, 2021 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.