Command works in terminal but not in bash script

Blair Fonville

If I want to delete everything in the current directory except for "myfile", I can use

rm -r !("myfile")

But if I put this in a script (called cleanup):

#!/bin/bash
rm -r !("myfile")

I get:

pi@raspberrypi:~/tmp $ ./cleanup
./cleanup: line 2: syntax error near unexpected token `('
./cleanup: line 2: `rm -r !("file2")'

If I run

ps -p $$

I can see that my terminal is using bash,

 PID TTY          TIME CMD
1345 pts/3    00:00:02 bash

so I'm unclear on what the problem is.


Notes:

  1. I realize that if the script actually worked, it would delete itself. So, my script really would be something more like: rm -r !("cleanup"|"myfile"), but the error message is the same either way.
  2. As shown by the blockquote, this is on a Raspbian OS (9 - stretch), which is Debian based.
  3. I feel like this question is bound to be a duplicate, but I can't find it. There is a similarly named question, but it is in regards to inheriting variables, and so doesn't address my issue.
Benjamin W.

The !(pattern-list) pattern is an extended glob. Many distros have it enabled for interactive shells, but not for non-interactive ones. You can check that with

$ shopt extglob
extglob         on
$ bash -c 'shopt extglob'
extglob         off

To fix your script, you have to turn it on: add

shopt -s extglob

at the beginning of it.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Running a command in a new terminal instance in a bash script

分類Dev

Bash-script as linux-service won't run, but executed from terminal works perfectly

分類Dev

Cannot run bash script from crontab when it works from command line bash

分類Dev

Running the script to invoke command in terminal

分類Dev

@reboot works with command but not script - Cron

分類Dev

Commands Working In Terminal, But Not In Bash Script

分類Dev

cp doesn't work in script but works in terminal

分類Dev

export variable works in terminal but not in shell script

分類Dev

bash_completion causing a 'command not found' in terminal

分類Dev

echo/printf incomplete command at bash terminal

分類Dev

-bash: 400:: command not found when open terminal

分類Dev

Execute sed command in a bash script

分類Dev

Bash script - unix - command not found

分類Dev

Running PHP works in Command Prompt but not Git Bash

分類Dev

exit a bash command in a script without exiting the script

分類Dev

Using bash variables in perl command in bash script

分類Dev

Same command that works in terminal does not work as Thunar Custom Action

分類Dev

Bash script to monitor file change and execute command

分類Dev

Combine sed commands into one command in bash script

分類Dev

How to execute a command within a bash script?

分類Dev

Linux Bash Script, Single Command But Multiple Lines?

分類Dev

bash: Why are () causing error in script but not on the command line?

分類Dev

Bash script using mysql command with variables failing

分類Dev

How to turn Diff command into bash script with prompt

分類Dev

bash script to check website content by curl command

分類Dev

Bash script can't find command if quoted

分類Dev

Bash script that runs a command with arguments and redirects

分類Dev

Running a shell script with and without "bash" command

分類Dev

Why doesn't my bash terminal recognize any command in the shell?

Related 関連記事

  1. 1

    Running a command in a new terminal instance in a bash script

  2. 2

    Bash-script as linux-service won't run, but executed from terminal works perfectly

  3. 3

    Cannot run bash script from crontab when it works from command line bash

  4. 4

    Running the script to invoke command in terminal

  5. 5

    @reboot works with command but not script - Cron

  6. 6

    Commands Working In Terminal, But Not In Bash Script

  7. 7

    cp doesn't work in script but works in terminal

  8. 8

    export variable works in terminal but not in shell script

  9. 9

    bash_completion causing a 'command not found' in terminal

  10. 10

    echo/printf incomplete command at bash terminal

  11. 11

    -bash: 400:: command not found when open terminal

  12. 12

    Execute sed command in a bash script

  13. 13

    Bash script - unix - command not found

  14. 14

    Running PHP works in Command Prompt but not Git Bash

  15. 15

    exit a bash command in a script without exiting the script

  16. 16

    Using bash variables in perl command in bash script

  17. 17

    Same command that works in terminal does not work as Thunar Custom Action

  18. 18

    Bash script to monitor file change and execute command

  19. 19

    Combine sed commands into one command in bash script

  20. 20

    How to execute a command within a bash script?

  21. 21

    Linux Bash Script, Single Command But Multiple Lines?

  22. 22

    bash: Why are () causing error in script but not on the command line?

  23. 23

    Bash script using mysql command with variables failing

  24. 24

    How to turn Diff command into bash script with prompt

  25. 25

    bash script to check website content by curl command

  26. 26

    Bash script can't find command if quoted

  27. 27

    Bash script that runs a command with arguments and redirects

  28. 28

    Running a shell script with and without "bash" command

  29. 29

    Why doesn't my bash terminal recognize any command in the shell?

ホットタグ

アーカイブ