0

I have this code:

#!/bin/bash 

#clear screen
clear 

# Ask for name of directory to create
echo "Please enter a directory name"
echo "that you wish to create"
read dir1

# create directory
mkdir $dir1

# change to the newly created directory
cd $dir1

# Tell user where he/she is
echo "This directory is called 'pwd'"


# create some files
touch file1 file2 file3


# put in some content 
echo "This is $dir1/file1" > file1
echo "This is $dir1/file2" > file2
echo "This is $dir1/file3" > file3



# announce file names
echo "The files in $dir1 are: "
ls -hl

# show the contents  of the files
echo "The content of the files are: "
cat file1
cat file2
cat file3


echo "Goodbye"

The line with echo "This directory is called 'pwd'" refuses to give the current working directory as expected. Please help me resolve this issue, I am currently learning BASH SHELL SCRIPTING

  • Jacob please check it again, and please who nailed the -1 on my question is it that bad? – George Udosen May 18 '16 at 12:12
  • Just a small hint - before creating a new dir via 'mkdir' it would be safer to check if that folder already exist. – dufte May 18 '16 at 12:25
1

You have to use

echo "This directory is called $(pwd)"

What you are trying to do is called command substitution. This used to be done using backticks (` `), but the currently recommended way is using $().

You can find a detailed explanation in this wiki page.

  • Thanks wayne_yux , the 411 on the 'backticks' helped me see the error in my interpretation of the source code – George Udosen May 18 '16 at 12:30
2

To get the current working directory as a variable:

echo $PWD

(capitals), or use the command

pwd

Then, using the variable, the line should be:

echo "This directory is called '$PWD'"

Output:

This directory is called /home/jacob

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.