Home > Enterprise >  What's the difference between adding a forwardslash and not in bash?
What's the difference between adding a forwardslash and not in bash?

Time:03-08

So I have this code

#!/bin/bash

> oldFiles.txt

files=$(grep " jane " ../data/list.txt | cut -d ' ' -f 3)

for i in $files ; do
 if test -e ~$i; then echo "$i" >> oldFiles.txt;
 fi
done

And when I run those code the oldFiles.txt still empty, but after I added a forwardslash in the if statement like in the codeblock below, the code works.

 if test -e ~/$i; then echo "$i" >> oldFiles.txt;

So what's the difference between adding a forwardslash and not? Thankyou.

CodePudding user response:

/ is the path delimiter. Since ~ expands to /home/user without a trailing foward slash, ~$i exapands to /home/userpath not /home/user/path.

  •  Tags:  
  • bash
  • Related