According to dirname --help, command dirname /usr/bin/sort will output /usr/bin
So I tried this:
1 #!/bin/bash
2
3 rawPath="${1}"
4 trimmed=dirname $rawPath
5 echo $trimmed
And ran the script:
bash ./trimPath.sh /files/data/swx_i/raw/2020/03
Output:
./trimPath.sh: line 5: /files/data/swx_i/raw/2020/03: is a directory
Is it because I store the path in the variable or something else?
GNU bash, version 4.1.2(2)-release (x86_64-redhat-linux-gnu)
trimmed=dirname $rawPathdoes not do what you think it does, you need to assign the output of the command in the variable.trimmed=$(dirname "$rawPath")now it is correct. Also shellcheck.net is your friend. - Jetchisel