0

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() { DIR="$1" 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!

4
  • 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
    CommentedMar 6, 2021 at 20:43
  • @Kusalananda I'm storing it in a vaiable because I also need it later.CommentedMar 7, 2021 at 4:40
  • 1
    So, how are you calling your function?
    – Kusalananda
    CommentedMar 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.CommentedMar 7, 2021 at 17:49

1 Answer 1

3

You have to pass the function directory name quoted.

$ type foo foo is a function foo () { dir="${1}"; cat "${dir}/test" } $ foo "folder 1" foobar $ foo folder 1 cat: folder/test: No such file or directory 
4
  • Not sure why you've got type foo...?CommentedMar 6, 2021 at 21:34
  • type foo shows content of foo function which mimics yours get-website.
    – Jiri B
    CommentedMar 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()CommentedMar 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.CommentedMar 7, 2021 at 17:48

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.