Redirect output to variable with whitespace

Asked

Viewed 129 times

0

I am making a script where I want to capture a path to the name of a user file and then use it, plus the extension, to redirect the output of a command.

example: Insert path: ~/Videos/file

I do it using "read-and path", and then rerouting the output like this: foo > ${path}.abc

But if that path has spaces, for example, ~/Área\ de\ Trabalho, go wrong.

How to capture the path so that it works even for those who have blanks?

Thanks in advance.

  • 1

    shell expands manifolds between quotation marks. Try: foo > ~/"$path".abc

  • Using the quotes works the path with spaces, but the use of ~ for the home if inserted by the user is lost.

  • see if my answer works...

1 answer

0


Try something like

foo >  "${path/\~/$HOME}.abc"

In which

  • apspas protect the spaces
  • ${var/pat/sub} replaces the "pat" pattern with the "sub" value in $var
  • Yes, it worked perfectly. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.