-1

I am creating a new file and trying to print out my current directory within a sentence. I want it to look like "My current directory is /.../.../.../". What can I do to get the output I am looking for? Any help or support is much appreciated.

bash3.2$ sciript test1.script
bash3.2$ cat -n > test1.1

I have tried doing several different things like...

echo "My current directory is" $pwd
echo "My current directory is $pwd"
pwd
...........
Marcus Burkhart
  • 95
  • 1
  • 14

1 Answers1

2

pwd is a command, not a variable, so you can't get its value using $pwd.

You're looking to perform command substitution, so use backticks or $(...) to evaluate commands in a double-quoted string:

echo "My current directory is `pwd`"
# or
echo "My current directory is $(pwd)"

More: https://unix.stackexchange.com/a/48393/4515

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 3
    Bash sets `$PWD`, so that's another option. – yellowantphil Jan 26 '15 at 22:28
  • My only problem is when I create (cat -n > test1.1) If I type in echo" My current directory is `pwd`" It types that exact sentence literally. The only way I can get it do show up outside of the specified file is "ctrl-d" then type it. – Marcus Burkhart Jan 26 '15 at 22:33
  • @yellowantphil, huh? Bash doesn't get between `cat` and its stdin in this use case (or in the case of any other command run without input redirection applied). Now, if it were `cat < – Charles Duffy Jan 26 '15 at 23:09