7

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
  • 2
    Do you perhaps have tree aliased to tree -F in your interactive shell? Commented Mar 2, 2016 at 4:01
  • @steeldriver Jesus Christ, How could you know that? I checked my ~/.profile and found exactly the same alias! Commented Mar 2, 2016 at 4:24

1 Answer 1

10

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.

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.