0

So I just executed < ./somefile.txt in a shell, and it doesn't appear to do anything (the text file has valid linux commands). So where are the contents of the text file being redirected to? I'm just guessing null, but I am not sure.

OS: Ubuntu 12.04

3

So I just executed "< ./somefile.txt" in linux shell,

Under bash, dash, and similar shells, that do not execute a command. That merely assigns somefile.txt to stdin. This goes nowhere because stdin is not used unless you supply a command.

To use stdin for something try, for example, cat:

cat <./somefile.txt

Since cat echoes stdin to stdout, you will see the file displayed in the terminal.

On the other hand, it you do want to execute the script, you can do that in a subshell as follows:

bash ./somefile.txt

Or, if (a) somefile.txt has its executable bit set (chmod +x somfile.txt) and (b) it is either plain sh compatible or has a proper shebang for the first line, such as #!/bin/bash, then:

./somefile.txt

Or, to execute it in the current shell (i.e. source it), run:

. ./somefile.txt

or:

source ./somefile.txt

Usually, one sources a script if one wants it to change the current working directory and other aspects of the current shell's environment. If you don't such side-effects, then don't source it; execute it in a subshell.

Advanced Usage

@StéphaneChazelas points out that <./somefile.txt is useful as a test of whether file ./somefile.txt is readable. For example, to exit with code 1 if ./somefile.txt is not readable:

<./somefile.txt || exit 2

This works because the shell can only assign ./somefile.txt to stdin if it is readable. If it isn't, the shell displays an error message and returns code 1. That code triggers the "or (||) clause which, in this case, would case the script to exit with code 2. The more standard and portable approach for the same test would be:

[ -r "./somefile.txt" ]  || exit 2
  • That's not completely true as zsh is a Bourne-like shell. – Stéphane Chazelas Aug 11 '14 at 5:50
  • Why would you source the file if it is just a script? Why not make it executable and ./somefile.txt? – Bernhard Aug 11 '14 at 5:58
  • @Bernhard Good point. I added a sentence for that. – John1024 Aug 11 '14 at 6:32
  • @StéphaneChazelas OK. I reworded that sentence. – John1024 Aug 11 '14 at 6:35
  • It can be used to check if a file is readable: < file || exit. Shorter version of [ -r "$file" ] || exit which also outputs an error message. – Stéphane Chazelas Aug 11 '14 at 7:18
2

It depends on the shell. With zsh, this is described under Section "REDIRECTIONS WITH NO COMMAND". By default, READNULLCMD will be used as the command, which defaults to more.

0

What you have is a null command with redirection.  You haven’t specified which shell you’re using.  vinc17 has addressed the handling of null commands in zsh.  In bash, a null command is simply a do-nothing; like an empty script.  You can infer this if you read between the lines in the Bash Reference Manual:

When a simple command is executed, the shell performs the following expansions, assignments, and redirections, from left to right.
                ︙
If no command name results, redirections are performed, but do not affect the current shell environment.  A redirection error causes the command to exit with a non-zero status.

(Emphasis added.)  In other words,

< filename

opens filename and then does nothing with it.  If the open fails, the shell outputs an error message.  Either way, exit status ($?) is set appropriately: 0 for success, 1 for failure.  This is comparable to

sleep 0 < filename

since sleep doesn’t read its standard input.

The slightly more interesting variant is

> filename

which creates filename and then does nothing with it.  This is a convenient way to create an empty file.

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.