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
To get the current working directory as a variable:
(capitals), or use the command
Then, using the variable, the line should be:
Output:
You have to use
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.