I'm writing a bash script that prints the contents of a file called website (using cat) that is inside a folder with a space in it's name, for example folder 1. but I can't make bash get the path correct, it always uses only the first word in the folder name, so cat folder/website instead of cat folder 1/website. so I want to replace the space with \ . I tried using sed and tr, but they didn't work.

here is my code:

function get-website() {
    website="$(cat "main/apps/$DIR/website")"

most of what I tried is here: Replace character X with character Y in a string with bash

thanks in advance!

  • 3
    How do you call your function in your code? Also, if you just want to print the contents of the file, why are you storing it in a variable?
    – Kusalananda
    Mar 6, 2021 at 20:43
  • @Kusalananda I'm storing it in a vaiable because I also need it later. Mar 7, 2021 at 4:40
  • 1
    So, how are you calling your function?
    – Kusalananda
    Mar 7, 2021 at 7:36
  • @Kusalananda that was the problem, how I was calling it: get-website $2 instead of get-website "$2". thanks a lot. Mar 7, 2021 at 17:49

1 Answer 1


You have to pass the function directory name quoted.

$ type foo
foo is a function
foo () 
    cat "${dir}/test"
$ foo "folder 1"
$ foo folder 1
cat: folder/test: No such file or directory
  • Not sure why you've got type foo...?
    – roaima
    Mar 6, 2021 at 21:34
  • type foo shows content of foo function which mimics yours get-website.
    – Jiri B
    Mar 7, 2021 at 8:08
  • I know what it does. I just don't see the relevance of it in your question. Just start with the third line, foo()
    – roaima
    Mar 7, 2021 at 9:16
  • the problem was how I was calling the function (as @Kusalananda asked) as well as how the code itself inside the function was. thanks a lot everyone. Mar 7, 2021 at 17:48

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.