/r/shell

Photograph via snooOG

News, articles, tips and tools covering shell scripting and command-line interfaces of all flavors.

News, articles, tips and tools covering shell scripting and command-line interfaces of all flavors.

Smokey says: use low-power lighting, such as LEDs, whenever possible to fight climate change! [see more tips]

Note: this subreddit is for the computer technology known as a shell (aka "terminal", "command prompt" or CLI). If you came for the fossil fuel dinosaur of the same name, too bad.

Resources:

Other subreddits you may like:

Does this sidebar need an addition or correction? Tell me here

/r/shell

5,256 Subscribers

1

Help with regex

How can i extract the first occurence of a date in a given .csv file.

example:

file.txt

-------------------

product, | date

Yamaha, 20/01/2021

Honda, 15/12/2021

--------------------

Any help, or maybe some reading i could use to get better at regex?

For Context:

I'm learning Linux for a internship program, and i have quite an amazing task.

Amongst all the steps to get the job done, which involves making a script that copy some file as backup, zips the backup file and creates a report.txt with some info inside and then schedule the script to be run at times. I need to extract expecific data, in a specific position at a file.

My first thought was that i could do something like this .

head -n 2 file.csv | tail -n 1 | grep -e "regexp"

Which would capture the first product, pipe to a grep and the regex would spill out only the date, buuuuut. I suck at regex.

The thing is, i am struggling so much with learning regex, that all i could do at this point was this regex...

^([0-9]{2}[\/]{1}){2}([0-9]{4})$

Which actualy matches the date format, but won't match the full string piped through, and won't capture the group with the date. This regex would only work if i pass in just a date "00/00/1234"

10 Comments
2024/11/13
23:06 UTC

3

BASH: Naming conventions for constants (readonly variables)

For variables and functions in Bash, the naming conventions seems to be snake_case. Is this also the case for all constants in Bash?

Or are primitive constants (like int, string) always SCREAMING_SNAKE CASE and non-primitive constants (like arrays) use snake_case?

2 Comments
2024/11/10
19:19 UTC

1

Has anyone set up Carapace successfully with PowerShell?

0 Comments
2024/10/30
16:50 UTC

1

crazy-complete: Generate shell auto-completion files using a single configuration file

Hello everyone!

I want to introduce my current project crazy-complete.

It is a tool that generates shell auto-completion files using a single configuration file.

Key Features

  • Simple configuration: The tool uses an easy-to-use YAML format
  • Multi shell support: The tool supports Bash, Fish and Zsh
  • Option types: All common option types are supported: long-options (--option), short-options (-o) and old-style-options (-option)
  • Argument types: Options with required arguments, with optional arguments and no arguments are supported
  • Mutually exclusive options: You can define groups of options that can't be used together
  • Repeatable options: You can control if an option may be repeated on commandline, or should appear only once
  • Positional arguments: You can easily define arguments for your program
  • Built-in completion types: The tool comes with these completions: file, directory, choices, value_list, range, signal, process, pid, command, user, group, service, variable, environment
  • Custom completions: If the built-ins aren't sufficient, there is the exec command to complete stuff based of a command's output
  • Conditional options/positionals: You can enable options and positionals based on conditions, like "is there an option present?" or "does an option have a specific value?"
  • Subommands: You can specify arbitrary levels of subcommands
  • Robust scripts: Unlike other generation tools, cracy-complete parses the whole commandline according to the specified options (instead of only looking at the last two args, for example)
  • Efficient scripts: Scripts were made with efficiency in mind. You pay only for what you use.
  • Well tested: There are over fifty tests per shell to ensure correct behaviour

Other Features

  • argparse Support: If your program is written in Python and uses the arparse-module, there is a chance that you can generate scripts (or YAML files) from the argparse.ArgumentParsers's definitions
  • Help text parser: If your program comes with a help text, there is a chance that you can generate YAML definition files by parsing it's output

If you're tired of maintaining completion scripts for different shells, crazy-complete may be for you.

Even if you decide against using generated scripts, the output of the tool may serve as an inspiration.

Let me know what you think. If you need support or have any questions or improvement-ideas, don't hesitate to ask!

And if you like the tool, please give it a star on GitHub

Here you can find an example definition file

1 Comment
2024/10/26
09:41 UTC

0

Demonstrate that Shell evaluates $RANDOM only once when used in the same arithmetic context

We need to ensure that both instances of $RANDOM are treated as a single evaluation. We need to inspect the evaluation process. An approach that captures the internal evaluation

Do you have any idea?

1 Comment
2024/09/28
11:06 UTC

0

cant change file permitions

https://preview.redd.it/okfomumiguqd1.png?width=740&format=png&auto=webp&s=aafbfcd3e49e9092acbf0ba1d3e18f82f1866d2e

I dont understand why i cant change certain file permitions (i can change some but not all).
I tried running sudo but it cant find the command and i cant seem to instal sudo with "su -".
Im running "git bash" on a windows... I also have 2 users, one personal, one for coding. Might it be the user settings? If so what and how do I change it... No one in my cluster has any info, help 💀🙏

8 Comments
2024/09/24
23:48 UTC

5

why do bash scripts start with #!

https://preview.redd.it/hhsgcz11ykqd1.png?width=434&format=png&auto=webp&s=04505409992b01f18dbe8e4e92a55e8692c76141

Hi, I'm just curious: what does this mean?

I know it's telling the machine to use bash, but like, why is it a "#!" for example? How/why was it decided that way?

11 Comments
2024/09/23
15:48 UTC

8

Is this the gas station’s Reddit page? If so..

On a road trip to Dallas, TX, we stopped at a Shell (the gas station) in Arkansas to hot to the bathroom. There was fucking BARNACLES on the SINK. BARNACLES! My hands felt dirtier after washing. Words cannot describe how infuriated I was, and still am, despite the encounter being a week ago. I needed to vent about this. This is not clickbait. This is REAL. Never going to a shell again.

9 Comments
2024/09/21
20:46 UTC

1

Cant seem to change permitions

https://preview.redd.it/1b439ub3hzpd1.png?width=580&format=png&auto=webp&s=d04320066c2e0d603681d000155c263b1f0bf615

Hi, new to coding just started studying shell.
Cant seem to change this files permitions, what am i doing wrong?
(i need to give g and o x permition, have already tried numeric and symbolic methods)

0 Comments
2024/09/20
15:34 UTC

0

need to read everything the pc is doing,

I need to know everything the pc is doing, for example I need to read it and be able to copy and paste the text to activate it, for example the if any app is open and anything I do on it I can see and still copy and paste to do the same, yes I know it's very weird, but if you smart guys or girls know I would love to know, sorry I didn't explain it well

3 Comments
2024/09/14
12:09 UTC

1

Opinion advice about my First zsh script

0 Comments
2024/09/10
18:05 UTC

2

I have two shell scripts, why one can bash and execute background, but the other one cannot?

shell script-1 :

#!/bin/bash

cd /(path)/DB/test

for line in $(cat ./test_list.txt); do
   ./../batch_download.sh -f ./${line} -p &
done

I closed the terminal, but the execution didn't stop. I checked, it is truly downloading files.

https://preview.redd.it/gr8g7lo6yind1.png?width=524&format=png&auto=webp&s=0c932f2d861baae7fdd27d196d3929af5e7d68cd

However,

shell script-2:

#!/bin/bash

cd /(path)/DB/test

./../batch_download.sh -f ./entry0.txt -p 

I cannot close the terminal.

https://preview.redd.it/yv59wl81zind1.png?width=627&format=png&auto=webp&s=91ff32783b5eee79a75c3eee74e99f8bd9b2d300

2 Comments
2024/09/08
05:56 UTC

1

An error occurred in 'before all hook ' when using shellSpec prevents all test executions

Hi All

I have implemented a unit test for a Shell using shellspec. And I am always thrown the above error in 'before all' and 'after all' both. Even though the log contains exit code 0 which basically indicating there is no error none of my tests are executing.
I have added extra logs and also redirected the errors but still I am facing this error and am out of options. I am using the latest version of Shellspec as well.

I am mocking git commands in my test script. But it is quite necessary for my tests as well.

I even checked for the relevent OS type in the setup method

 # Determine OS type
    OS_TYPE=$(uname 2>/dev/null || echo "Unknown")

    case "$OS_TYPE" in
        Darwin|Linux)
            TMP_DIR="/tmp"
            ;;
        CYGWIN*|MINGW*|MSYS*)
            if command -v cygpath >/dev/null 2>&1; then
                TMP_DIR="$(cygpath -m "${TEMP:-/tmp}")"
            else
                echo "Error: cygpath not found" >&2
                exit 1
            fi
            ;;
        *)
            echo "Error: Unsupported OS: $OS_TYPE" >&2
            exit 1
            ;;
    esac

Any guidance is immensely appreciated.

0 Comments
2024/09/05
17:37 UTC

5

My Command Line Todo List

I recently published a comment about my todo list on HN and a few people asked about it. I decided to do a quick write-up of the simple commands I put together to accomplish this in the shell (zsh in my case but it likely works in bash too).

https://www.makervoyage.com/todo

3 Comments
2024/08/16
13:41 UTC

1

What does peace of mind look like for a shell/Terminal layer software developer /programmer?

Asking for a brother who is building tools to help other shell/Terminal layer programmers. Thank you.

1 Comment
2024/08/12
00:30 UTC

0

A strange way ARM64 beat AMD64 with XZ 😅 but why?

No, something different than you think... xz performs up to 2% better on ARM64 than AMD64 with the same settings and I have double checked the version, it is the same! gzip on the other hand... no change at all. Also interesting, xz -1 and AMD64 is WAY faster and better than gzip except memory usage but even 100MByte are pretty tame nowadays. So basically gzip is now superfluous in ever aspect, even on my 1990 Amiga with bash and xz I can unpack those Archives, and faster than xz too. And no, I didn't benchmark the Amiga too. I only remember it from the last time I booted it up.

amd64:

578339776 test

40926066 test.gz1

40024392 test.gz2

39818807 test.gz3

36787548 test.gz4

35988603 test.gz5

35188701 test.gz6

35100687 test.gz7

32072861 test.gz8

31762517 test.gz9

23958376 test.xz1

22207696 test.xz2

21483232 test.xz3

25643828 test.xz4

24781276 test.xz5

24459080 test.xz6

24041684 test.xz7

23434612 test.xz8

22865948 test.xz9

15539300 test.xz9e

arm64: 578339776 test

40926066 test.gz1

40024392 test.gz2

39818807 test.gz3

36787548 test.gz4

35988603 test.gz5

35188701 test.gz6

35100687 test.gz7

32072861 test.gz8

31762517 test.gz9

23515536 test.xz1

21855000 test.xz2

21210096 test.xz3

25397736 test.xz4

24562780 test.xz5

24230096 test.xz6

23841668 test.xz7

23250764 test.xz8

22824292 test.xz9

If you are interested in the rest, the first comma column is i7-6700k, the second comma column is Pi400:

578339776 test

for I in 1 2 3 4 5 6 7 8 9 9e; do for A in 1 2 3; do echo "RUNNING $I - $A"; ( time ( xz -$I <test >test.xz$I ) ) 2>&1 | grep real; done; done

xz -1 1,9 35,9 23958376 test.xz1

xz -2 2,8 49,6 22207696 test.xz2

xz -3 4,4 73,1 21483232 test.xz3

xz -4 5,0 88,9 25643828 test.xz4

xz -5 8,1 143,0 24781276 test.xz5

xz -6 13,2 246,2 24459080 test.xz6

xz -7 15,0 24041684 test.xz7

xz -8 21,5 23434612 test.xz8

xz -9 32,9 22865948 test.xz9

xz -9e 155,4 15539300 test.xz9e

for I in 1 2 3 4 5 6 7 8 9 9e; do for A in a b c; do echo "RUNNING $I$A"; ( time ( gzip -$I <test >test.gz$I ) ) 2>&1 | grep real; done; done

gzip -1 2,8 8,4 40926066 test.gz1

gzip -2 2,9 8,5 40024392 test.gz2

gzip -3 3,0 8,4 39818807 test.gz3

gzip -4 3,7 10,7 36787548 test.gz4

gzip -5 3,8 11,2 35988603 test.gz5

gzip -6 4,6 14,5 35188701 test.gz6

gzip -7 5,2 16,8 35100687 test.gz7

gzip -8 7,8 27,2 32072861 test.gz8

gzip -9 9,2 34,0 31762517 test.gz9

I think that is self-explanatory.

0 Comments
2024/08/08
06:02 UTC

1

Zip contents of a directory excluding the directory

Hello, I am new to scripting and trying to make a script that will make it easier to use yuescript to code with the love2d framework. I am currently facing an issue with the zip command.

yue -t lua src
rm game.love
zip -r game.love lua
love game.love

The probem is that the root is lua directory and not main.lua, using lua/* doesn't seem to work, the rest works as intended

2 Comments
2024/07/22
15:36 UTC

1

How To Make Your Boring Terminal So Much Better

Here is how to customize your boring default terminal to boost your productivity. Discover useful terminal commands for navigation and shortcuts to enhance your terminal experience.

0 Comments
2024/07/07
12:48 UTC

4

etcsh: an enhanced version of the original tcsh

etcsh is an enhanced version of the original tcsh, which is a version of the original Berkeley C shell implementing file name completion and command line editing.

etcsh features the following enhancements and fixes over the original tcsh.

• Function declarations;

• variable assignment from pipes and redirections;

• comments on interactive sessions;

• tell whether stdin is empty or not;

• variable expansion on expressions corrected;

• piped built-ins are forked;

• missing test built-in implemented;

• read-write variable flag;

exit shall cancel lists.

More is to come. Feedbacks and contributions are much appreciated.

0 Comments
2024/06/16
22:57 UTC

3

Shameless plug: pieshell: python for shell scripting and as an interactive shell

I have written a shell that uses the Python language as the scripting language: it's a shell environment that combines the expressiveness of shell pipelines with the power of python iterators.

It can be used in two major ways:

  • As an interactive shell replacing e.g. bash
  • As an ordinary python module replacing e.g. subprocess.Popen

Obligatory example:

140:/home/redhog/Projects/beta/pieshell >>> for x in ls(-a):
...   if x.endswith('.py'):
...      print x
... 
setup.py

https://github.com/redhog/pieshell

0 Comments
2024/06/14
17:05 UTC

6

First experience with the rc shell

Hi all,

after using fish for about 5 years, I have tried the rc shell. I would like to share my experience so far: rc shell instead of bash or fish. If there are any other rc users in this sub, maybe you can help me with some of the open points (like how to copy the current command buffer or better integration of fzf).

0 Comments
2024/05/29
10:27 UTC

0

Print value stored in variable

VAR1=123 VAR2=456 VAR3=789

VARIABLES="VAR1 VAR2 VAR3"

for i in $VARIABLES do echo "$i = $$i #something like this

done

Expected output: VAR1 = 123

4 Comments
2024/05/18
16:18 UTC

1

Windows Terminal | Backup and Sync Feature Request

Hi
I'm sure some of you have ever wished to be able to just easily sync changes from one windows terminal instance to another one, for example a secondary laptop or a work device.

If so, consider adding a thumbs-up or your comment to this feature request, thanks!

https://github.com/microsoft/terminal/issues/17277

0 Comments
2024/05/16
13:45 UTC

Back To Top