1

Hello so today i was playing around with my shell script and figured to make it more user friendly i would make it so the file extension of file was automatically added.

for example say the user wants to search a file using grep but first they must type in thhe name of the file in this case lets say file.txt what i want to do is automatically add on the .txt so the user only needs to type in "file"

here is what i have so far but this does not work:

echo "Current .txt files "
        ls -R |grep .txt
        echo "--------------------------------------------------------------------------------"
                echo -n "Please select a file to search in: "
                read fileName
                file=$fileName.txt

i thought in this case since i am appending an extension on to the end of the variable name but this has not worked.

1
  • grep .txt will likely match more than you wanted; try grep '\.txt$'. Generally, consider using find` for enumerating files in directory subtrees, because it is much more flexible; the equivalent of your command using find is find . -type f -name '*.txt' -exec basename {} + Commented Apr 21, 2014 at 22:21

2 Answers 2

3

Put quotes around it:

file="$filename.txt"

EDIT: As it happens, this answer is incorrect. See the comments below and the other answer.

Sign up to request clarification or add additional context in comments.

4 Comments

Strictly speaking, you don't need quotes on variable assignments (reference: "Word splitting is not performed [...]. Filename expansion is not performed.") -- whereever $file is used, that's where the quotes are needed.
obviously. You're describing gnu.org/software/bash/manual/… not variable assignment.
Oh, I see what you mean. If it's a variable whose contents contains spaces it'll work without the quotes. Interesting.
Here's a demo: touch foobar: x=foo*; echo "$x"; echo $x
2

Your code should work. See below-

Contents of test.sh:

#!/bin/bash

echo "Current .txt files "
        ls -R |grep .txt
        echo "--------------------------------------------------------------------------------"
                echo -n "Please select a file to search in: "
                read fileName
                file=$fileName.txt

echo "You are searching for $file"
ls -l "$file"

Test run:

$ ./test.sh
Current .txt files
p1.txt
t1.txt
t2.txt
--------------------------------------------------------------------------------
Please select a file to search in: t2
You are searching for t2.txt
-rw-r----- 1 d1rld1f1 d1rld1f1 4 Apr 20 12:41 t2.txt
$

BTW, it is generally advisable to enclose variable names in double quotes. This prevents reinterpretation of all special characters within the quoted string.

1 Comment

Note that ls -R | grep .txt returns text files anywhere in the current folder's subtree , whereas your test run with ls -l only happens to work if the specified text file is directly in the current folder.

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.