0

Let's say i have a string

r="part1 - part2"

i can do

PART1=${r%%-*}
PART2=${r##*-}

to get each part. PART1 will also get the white space in front of it and PART2 will get the white space behind it.

However, since I am trying to use these two varibles as part of the path and it will become a problem since there is a space in it.

So is there a way to use the exact same method to escape the white space so it will also remove the white space behind/after it?

123
  • 10,778
  • 2
  • 22
  • 45
Spencer Ovetsky
  • 170
  • 6
  • 14

1 Answers1

0

If you want to split a string based on a hyphen as a separator you can do this:

IFS=- read first second <<< "$r"
$ printf '%q\n' "$first"
part1\ # Note the space here
$ printf '%q\n' "$second"
\ part2

If you want to get only the words in a string you can do this:

$ read first separator second <<< "$r"
$ printf '%q\n' "$first"
part1
$ printf '%q\n' "$separator"
-
$ printf '%q\n' "$second"
part2

That said, spaces (and other special characters such as high Unicode, newlines, punctuation etc.) in file paths is a particularly thorny subject for shell programming, but they are perfectly valid. The first rule of handling special paths is quoting properly. Once you get that right, you're 90% of the way towards supporting basically any valid *nix path. The other 10% involve various tricks like using -- as a separator between arguments and paths if you need to parse arguments, starting relative paths with ./ outside scripts and "$(cd "$(dirname "${BASH_SOURCE\[0\]}")" && pwd)" inside scripts, looping over paths using while, and escaping special characters using $'' quotes (see man bash). I would definitely recommend learning these and writing test cases for your script with incrementally more complex file names to guarantee that it works.

l0b0
  • 55,365
  • 30
  • 138
  • 223
  • I think they just want to remove the spaces. – 123 Apr 16 '17 at 14:13
  • So i have a shell script. I have to split a filename into two variables. But the filename contains both hyphen and space. Such as "first - second". Using my old meta, I will get "first " with space after it and " second" with space in front of the word. Now I need to use these two varibles in such way that I will move a file to a folder named as the first variable and rename the file as a second variable.so I would do mv "file" "Part1/Part2" since it has space in it, it throw me off. I got "mv cannot move 'testFile - TestDir.txt' to ''testFile / TestDir.txt' : No such file or directory. – Spencer Ovetsky Apr 16 '17 at 14:40
  • I have noticed that the part where it says 'testFile / TestDir.txt' has a space in front and after the slash. So i was thinking i didn't handle the space in the variable. – Spencer Ovetsky Apr 16 '17 at 14:41
  • My script so hard, only handles a file name without space. So "testFile-TestDir.txt" will work, but "testFile - TestDir.txt" won't work – Spencer Ovetsky Apr 16 '17 at 14:42
  • Thanks, i got it, i was over-complicating the problem! – Spencer Ovetsky Apr 16 '17 at 15:19