Ubuntu – Why is bash denying the shell script permission to open a text file with default text editor

bashcommand linepermissionsscripts

I am in:
GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)

the script is: (note.sh)

 #! /bin/bash

edit="edit"

if [[ $edit = $1 ]]
then
    touch ~/.notes/"$2".txt
    $EDITOR ~/.notes/"$2".txt
else
    tree ~/.notes
fi

I was hoping
if I input in bash:
./note.sh
I get output as if i had typed
tree ~/.notes
But i want this script to basically accept arguments, so
if I input
./note.sh edit new_note
then if new_note.txt does not exist,
touch ~/.notes/new_note.txt
then
(Gedit for me) text editor opening new_note.txt in terminal for editing

the else statement works, but
./note.sh edit new_note returns

./note.sh: line 10: /home/username/.notes/testnote.txt: Permission denied

It does the touch but not the editor.
What is being denied permission here?

Thanks in advance! I am very new to both shell scripting and askubuntu and much appreciate any help

Best Answer

In bash the variable $EDITOR is not set by default. However, there is a command that will invoke the default editor.

For this command it is:

editor <filename>

To set the command to your choice:

sudo update-alternatives --config editor

Example:

terrance@terrance-ubuntu:~$ sudo update-alternatives --config editor
There are 3 choices for the alternative editor (providing /usr/bin/editor).

  Selection    Path               Priority   Status
------------------------------------------------------------
  0            /bin/nano           40        auto mode
  1            /bin/ed            -100       manual mode
  2            /bin/nano           40        manual mode
* 3            /usr/bin/vim.tiny   10        manual mode

Press <enter> to keep the current choice[*], or type selection number:

After choosing your default editor, then all you have to do to call it in your script is:

editor ~/.notes/"$2".txt

Hope this helps!

Related Question