3

I have a shell script executes tree command

$ cat _tree.sh 
#!/bin/sh
tree -L 2 --charset ascii -I "_tree.sh|LICENSE|README.md|node_modules|nbproject"
$ sh _tree.sh 
.
|-- bower.json
|-- dpl
|-- dst
|-- gulpfile.js
|-- package.json
`-- src
    |-- fonts
    |-- images
    |-- scripts
    `-- styles

7 directories, 3 files
$

When I execute the command directly,

$ tree -L 2 --charset ascii -I "_tree.sh|LICENSE|README.md|node_modules|nbproject"
.
|-- bower.json
|-- dpl/
|-- dst/
|-- gulpfile.js
|-- package.json
`-- src/
    |-- fonts/
    |-- images/
    |-- scripts/
    `-- styles/

7 directories, 3 files
$

The forwarding slashes(/) are appended. How can I make the _tree.sh file do this?

  • 2
    Do you perhaps have tree aliased to tree -F in your interactive shell? – steeldriver Mar 2 '16 at 4:01
  • @steeldriver Jesus Christ, How could you know that? I checked my ~/.profile and found exactly the same alias! – Jin Kwon Mar 2 '16 at 4:24
4

To append trailing slashes for directories, simply revise your code to include the -F option, in _tree.sh:

tree -F -L 2 --charset ascii -I "_tree.sh|LICENSE|README.md|node_modules|nbproject"

Explanation

The tree program (for example version 1.7.0) does not append trailing slashes by default. As @steeldriver points out, it may only be due to -F option enabled somewhere on your system, such as within ~/.bashrc or ~/.bash_aliases defined as an alias, that makes it so you see trailing slashes when you run tree on the terminal. To have trailing slashes in your scripts too, simply add -F option to your tree command.

| improve this answer | |

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.