24

In order to test if a file exists I do:

if [ -f $FILE ];

but it doesn't work if $FILE is a named pipe, e.g. ls -l pipename shows a pipe with p attribute:

prw-r--r-- 1 usr grp 0 Nov 26 02:22 pipename

How to test if a named pipe exists?

1

3 Answers 3

27

You could use the -p test

if [[ -p $pipe ]]

or

if [ -p "$pipe" ]
7

The friendly man page lists several file test operators, including:

-e file
       True if file exists.

and

-f file
       True if file exists and is a regular file.

and

-p file
       True if file exists and is a named pipe (FIFO).

Don't just use -f all the time; use the one that does the thing you mean.

1
  • test's manpage, I guess ? (I used to look them up in e.g. bash manpage, but that's far from being efficient)
    – PypeBros
    Commented Dec 14, 2017 at 12:55
4

You can try like this:

if [[  -p $pipe ]]

or you can simply try by removing the [] like this:

if [ -p "$pipe" ]

Also check Bash Conditional Expressions and Bash Shell: Check File Exists or Not

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.