Empty string is a file? ( if [ ! -f “” ] )

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-1
down vote

favorite












The script is called isFile.sh and looks like this:




#!/bin/sh

echo $1
echo $2

if [ ! -f $1 ]; then
echo "$1 (arg1) is not a file"
fi

if [ ! -f $2 ]; then
echo "$2 (arg2) is not a file"
fi


First I created a file by doing touch file.exist.



And I ran bash isFile.sh file.exist file.notexist
The output was:




file.exist



file.notexist



file.notexist (arg2) is not a file





Then I ran bash isFile.sh "" file.notexist
The output was:




(# empty line)



file.notexist



file.notexist (arg2) is not a file




Expected output is:




(# empty line)



file.notexist



(arg1) is not a file



file.notexist (arg2) is not a file




Can somebody explain why?










share|improve this question























  • Are you sure about echo$1? I would think it is echo $1 (better cut an paste). And welcome!
    – Volker Siegel
    Aug 7 at 6:01










  • @ Volker Siegel, Sure it's echo $1, I edited my question, thanks.
    – SoloKyo
    Aug 7 at 6:04















up vote
-1
down vote

favorite












The script is called isFile.sh and looks like this:




#!/bin/sh

echo $1
echo $2

if [ ! -f $1 ]; then
echo "$1 (arg1) is not a file"
fi

if [ ! -f $2 ]; then
echo "$2 (arg2) is not a file"
fi


First I created a file by doing touch file.exist.



And I ran bash isFile.sh file.exist file.notexist
The output was:




file.exist



file.notexist



file.notexist (arg2) is not a file





Then I ran bash isFile.sh "" file.notexist
The output was:




(# empty line)



file.notexist



file.notexist (arg2) is not a file




Expected output is:




(# empty line)



file.notexist



(arg1) is not a file



file.notexist (arg2) is not a file




Can somebody explain why?










share|improve this question























  • Are you sure about echo$1? I would think it is echo $1 (better cut an paste). And welcome!
    – Volker Siegel
    Aug 7 at 6:01










  • @ Volker Siegel, Sure it's echo $1, I edited my question, thanks.
    – SoloKyo
    Aug 7 at 6:04













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











The script is called isFile.sh and looks like this:




#!/bin/sh

echo $1
echo $2

if [ ! -f $1 ]; then
echo "$1 (arg1) is not a file"
fi

if [ ! -f $2 ]; then
echo "$2 (arg2) is not a file"
fi


First I created a file by doing touch file.exist.



And I ran bash isFile.sh file.exist file.notexist
The output was:




file.exist



file.notexist



file.notexist (arg2) is not a file





Then I ran bash isFile.sh "" file.notexist
The output was:




(# empty line)



file.notexist



file.notexist (arg2) is not a file




Expected output is:




(# empty line)



file.notexist



(arg1) is not a file



file.notexist (arg2) is not a file




Can somebody explain why?










share|improve this question















The script is called isFile.sh and looks like this:




#!/bin/sh

echo $1
echo $2

if [ ! -f $1 ]; then
echo "$1 (arg1) is not a file"
fi

if [ ! -f $2 ]; then
echo "$2 (arg2) is not a file"
fi


First I created a file by doing touch file.exist.



And I ran bash isFile.sh file.exist file.notexist
The output was:




file.exist



file.notexist



file.notexist (arg2) is not a file





Then I ran bash isFile.sh "" file.notexist
The output was:




(# empty line)



file.notexist



file.notexist (arg2) is not a file




Expected output is:




(# empty line)



file.notexist



(arg1) is not a file



file.notexist (arg2) is not a file




Can somebody explain why?







files string test






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 7 at 6:04

























asked Aug 7 at 4:44









SoloKyo

264




264











  • Are you sure about echo$1? I would think it is echo $1 (better cut an paste). And welcome!
    – Volker Siegel
    Aug 7 at 6:01










  • @ Volker Siegel, Sure it's echo $1, I edited my question, thanks.
    – SoloKyo
    Aug 7 at 6:04

















  • Are you sure about echo$1? I would think it is echo $1 (better cut an paste). And welcome!
    – Volker Siegel
    Aug 7 at 6:01










  • @ Volker Siegel, Sure it's echo $1, I edited my question, thanks.
    – SoloKyo
    Aug 7 at 6:04
















Are you sure about echo$1? I would think it is echo $1 (better cut an paste). And welcome!
– Volker Siegel
Aug 7 at 6:01




Are you sure about echo$1? I would think it is echo $1 (better cut an paste). And welcome!
– Volker Siegel
Aug 7 at 6:01












@ Volker Siegel, Sure it's echo $1, I edited my question, thanks.
– SoloKyo
Aug 7 at 6:04





@ Volker Siegel, Sure it's echo $1, I edited my question, thanks.
– SoloKyo
Aug 7 at 6:04











1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










The issue is that [ ! -f $1 ] becomes [ ! -f ] after expansion (and not [ ! -f "" ] as you thought!), so instead if checking if a given file exists, [ checks if the -f string is empty or not. It's not empty, but thanks to ! the final exit code is 1, thus the echo command is not executed.



That's why you need to quote your variables in POSIX shells.



Related questions:



  • Why does my shell script choke on whitespace or other special characters?

  • Security implications of forgetting to quote a variable in bash/POSIX shells





share|improve this answer




















  • Thanks for mention me add quotes on variables, but still I can't get the output right. Means I still get the same output after I double quoted my variable in the IF condition. Change $1 to " " or ' ' doesn't work either.
    – SoloKyo
    Aug 7 at 5:16










  • @KitisinKyo, do you mean that bash -c '[ -f "" ] && echo yes' outputs yes for you? What system is that?
    – Stéphane Chazelas
    Aug 7 at 5:37










  • @Stéphane Chazelas, Nope, bash -c '[ -f "" ] && echo yes' had no output. I'm using CentOS 7
    – SoloKyo
    Aug 7 at 5:58











  • @KitisinKyo, yet you're saying in the comment above that if [ ! -f "" ]; then echo ...; fi outputs nothing. Try running the script with bash -x to see what happens.
    – Stéphane Chazelas
    Aug 7 at 6:00










  • @Stéphane Chazelas, I edited a wrong script, the code worked, I was dumb.
    – SoloKyo
    Aug 7 at 6:13










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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460950%2fempty-string-is-a-file-if-f%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote



accepted










The issue is that [ ! -f $1 ] becomes [ ! -f ] after expansion (and not [ ! -f "" ] as you thought!), so instead if checking if a given file exists, [ checks if the -f string is empty or not. It's not empty, but thanks to ! the final exit code is 1, thus the echo command is not executed.



That's why you need to quote your variables in POSIX shells.



Related questions:



  • Why does my shell script choke on whitespace or other special characters?

  • Security implications of forgetting to quote a variable in bash/POSIX shells





share|improve this answer




















  • Thanks for mention me add quotes on variables, but still I can't get the output right. Means I still get the same output after I double quoted my variable in the IF condition. Change $1 to " " or ' ' doesn't work either.
    – SoloKyo
    Aug 7 at 5:16










  • @KitisinKyo, do you mean that bash -c '[ -f "" ] && echo yes' outputs yes for you? What system is that?
    – Stéphane Chazelas
    Aug 7 at 5:37










  • @Stéphane Chazelas, Nope, bash -c '[ -f "" ] && echo yes' had no output. I'm using CentOS 7
    – SoloKyo
    Aug 7 at 5:58











  • @KitisinKyo, yet you're saying in the comment above that if [ ! -f "" ]; then echo ...; fi outputs nothing. Try running the script with bash -x to see what happens.
    – Stéphane Chazelas
    Aug 7 at 6:00










  • @Stéphane Chazelas, I edited a wrong script, the code worked, I was dumb.
    – SoloKyo
    Aug 7 at 6:13














up vote
4
down vote



accepted










The issue is that [ ! -f $1 ] becomes [ ! -f ] after expansion (and not [ ! -f "" ] as you thought!), so instead if checking if a given file exists, [ checks if the -f string is empty or not. It's not empty, but thanks to ! the final exit code is 1, thus the echo command is not executed.



That's why you need to quote your variables in POSIX shells.



Related questions:



  • Why does my shell script choke on whitespace or other special characters?

  • Security implications of forgetting to quote a variable in bash/POSIX shells





share|improve this answer




















  • Thanks for mention me add quotes on variables, but still I can't get the output right. Means I still get the same output after I double quoted my variable in the IF condition. Change $1 to " " or ' ' doesn't work either.
    – SoloKyo
    Aug 7 at 5:16










  • @KitisinKyo, do you mean that bash -c '[ -f "" ] && echo yes' outputs yes for you? What system is that?
    – Stéphane Chazelas
    Aug 7 at 5:37










  • @Stéphane Chazelas, Nope, bash -c '[ -f "" ] && echo yes' had no output. I'm using CentOS 7
    – SoloKyo
    Aug 7 at 5:58











  • @KitisinKyo, yet you're saying in the comment above that if [ ! -f "" ]; then echo ...; fi outputs nothing. Try running the script with bash -x to see what happens.
    – Stéphane Chazelas
    Aug 7 at 6:00










  • @Stéphane Chazelas, I edited a wrong script, the code worked, I was dumb.
    – SoloKyo
    Aug 7 at 6:13












up vote
4
down vote



accepted







up vote
4
down vote



accepted






The issue is that [ ! -f $1 ] becomes [ ! -f ] after expansion (and not [ ! -f "" ] as you thought!), so instead if checking if a given file exists, [ checks if the -f string is empty or not. It's not empty, but thanks to ! the final exit code is 1, thus the echo command is not executed.



That's why you need to quote your variables in POSIX shells.



Related questions:



  • Why does my shell script choke on whitespace or other special characters?

  • Security implications of forgetting to quote a variable in bash/POSIX shells





share|improve this answer












The issue is that [ ! -f $1 ] becomes [ ! -f ] after expansion (and not [ ! -f "" ] as you thought!), so instead if checking if a given file exists, [ checks if the -f string is empty or not. It's not empty, but thanks to ! the final exit code is 1, thus the echo command is not executed.



That's why you need to quote your variables in POSIX shells.



Related questions:



  • Why does my shell script choke on whitespace or other special characters?

  • Security implications of forgetting to quote a variable in bash/POSIX shells






share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 7 at 4:58









nxnev

2,4872423




2,4872423











  • Thanks for mention me add quotes on variables, but still I can't get the output right. Means I still get the same output after I double quoted my variable in the IF condition. Change $1 to " " or ' ' doesn't work either.
    – SoloKyo
    Aug 7 at 5:16










  • @KitisinKyo, do you mean that bash -c '[ -f "" ] && echo yes' outputs yes for you? What system is that?
    – Stéphane Chazelas
    Aug 7 at 5:37










  • @Stéphane Chazelas, Nope, bash -c '[ -f "" ] && echo yes' had no output. I'm using CentOS 7
    – SoloKyo
    Aug 7 at 5:58











  • @KitisinKyo, yet you're saying in the comment above that if [ ! -f "" ]; then echo ...; fi outputs nothing. Try running the script with bash -x to see what happens.
    – Stéphane Chazelas
    Aug 7 at 6:00










  • @Stéphane Chazelas, I edited a wrong script, the code worked, I was dumb.
    – SoloKyo
    Aug 7 at 6:13
















  • Thanks for mention me add quotes on variables, but still I can't get the output right. Means I still get the same output after I double quoted my variable in the IF condition. Change $1 to " " or ' ' doesn't work either.
    – SoloKyo
    Aug 7 at 5:16










  • @KitisinKyo, do you mean that bash -c '[ -f "" ] && echo yes' outputs yes for you? What system is that?
    – Stéphane Chazelas
    Aug 7 at 5:37










  • @Stéphane Chazelas, Nope, bash -c '[ -f "" ] && echo yes' had no output. I'm using CentOS 7
    – SoloKyo
    Aug 7 at 5:58











  • @KitisinKyo, yet you're saying in the comment above that if [ ! -f "" ]; then echo ...; fi outputs nothing. Try running the script with bash -x to see what happens.
    – Stéphane Chazelas
    Aug 7 at 6:00










  • @Stéphane Chazelas, I edited a wrong script, the code worked, I was dumb.
    – SoloKyo
    Aug 7 at 6:13















Thanks for mention me add quotes on variables, but still I can't get the output right. Means I still get the same output after I double quoted my variable in the IF condition. Change $1 to " " or ' ' doesn't work either.
– SoloKyo
Aug 7 at 5:16




Thanks for mention me add quotes on variables, but still I can't get the output right. Means I still get the same output after I double quoted my variable in the IF condition. Change $1 to " " or ' ' doesn't work either.
– SoloKyo
Aug 7 at 5:16












@KitisinKyo, do you mean that bash -c '[ -f "" ] && echo yes' outputs yes for you? What system is that?
– Stéphane Chazelas
Aug 7 at 5:37




@KitisinKyo, do you mean that bash -c '[ -f "" ] && echo yes' outputs yes for you? What system is that?
– Stéphane Chazelas
Aug 7 at 5:37












@Stéphane Chazelas, Nope, bash -c '[ -f "" ] && echo yes' had no output. I'm using CentOS 7
– SoloKyo
Aug 7 at 5:58





@Stéphane Chazelas, Nope, bash -c '[ -f "" ] && echo yes' had no output. I'm using CentOS 7
– SoloKyo
Aug 7 at 5:58













@KitisinKyo, yet you're saying in the comment above that if [ ! -f "" ]; then echo ...; fi outputs nothing. Try running the script with bash -x to see what happens.
– Stéphane Chazelas
Aug 7 at 6:00




@KitisinKyo, yet you're saying in the comment above that if [ ! -f "" ]; then echo ...; fi outputs nothing. Try running the script with bash -x to see what happens.
– Stéphane Chazelas
Aug 7 at 6:00












@Stéphane Chazelas, I edited a wrong script, the code worked, I was dumb.
– SoloKyo
Aug 7 at 6:13




@Stéphane Chazelas, I edited a wrong script, the code worked, I was dumb.
– SoloKyo
Aug 7 at 6:13

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460950%2fempty-string-is-a-file-if-f%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay