Questions tagged [shell]

The shell is Unix's command-line interface. You can type commands in a shell interactively, or write scripts to automate tasks. Use this tag for questions applying to /bin/sh and most compatible shells (ash, bash, ksh, zsh, …). For shell scripts with errors, please check them in http://shellcheck.net before posting here.

Filter by
Sorted by
Tagged with
5 votes
0 answers
143 views

Ubuntu ls doesn't show all the files, but when I "ls filename", it shows up? [closed]

I am using PHP 7.3 to move some files around, in a larger app. The PHP 7 is running on Ubuntu 20.04.4 in a wsl2 container in Windows 10. On reading through other questions, it seems this is probably ...
user avatar
  • 51
0 votes
0 answers
40 views

Why is shell/terminal printing extra new line. What can be done to prevent it?

root@kali-linux:~# echo -ne "hi" hi root@kali-linux:~# echo "hi" hi root@kali-linux:~# I am using bash shell, xfce + i3wm + stterm in kali. On previous version of kali, there ...
user avatar
  • 113
1 vote
1 answer
40 views

Is there a commonly used environment variable or configuration setting for enabling widespread application verbosity?

Sometimes I write scripts where I want every called application to be verbose as can be for the sake of logging. This often includes typing --verbose quite a bit. Other than the -v and equivalent --...
user avatar
  • 467
-1 votes
1 answer
50 views

One master ksh file to call 2 ksh script files [duplicate]

I have 2 ksh (korn shell script) : file1.ksh and file2.ksh; and I want to have a master file ksh script that will call the 2 files; what's the code I have to put in my master file? Thanks
user avatar
  • 1
1 vote
0 answers
22 views

Counter-intuitive date command [duplicate]

I've been using date in a script in order to add one year to a specific date the following way: date '+%Y-%m-%d %H:%M:%S' -d "$DATE + 1 year" In this example, DATE looks like 2022-05-11 14:...
user avatar
1 vote
3 answers
328 views

What is a command? [closed]

Clear thinking and clear communication are facilitated when different terms are used to represent different concepts. This is particularly useful when the 2 concepts are very similar but different. We ...
user avatar
  • 336
0 votes
2 answers
132 views

echo file names containing 'a' and 'b' in either order [duplicate]

I have a list of files as follows: $ echo * add akfefg aka ab ba I want to echo file names containing both 'a' and 'b' in either order. I tried echo *[ab]*[ba]* aka ab ba and it also prints 'aka'. I'...
user avatar
  • 125
0 votes
2 answers
49 views

How to execute the next command in pipe only if previous have at least one result?

So, I have the following command $(docker container ls -a -q) | docker container stop to stop all my docker containers, which works great! Except when I've just wiped clean all of them: "docker ...
user avatar
0 votes
0 answers
47 views

Accessing environment variable set with "bash -c 'export FOO=BAR'"

I am trying to set environment variables from Bash as follow bash -c 'export FOO=BAR' but when I try to read the previously set variable using echo $FOO or bash -c 'echo $FOO' I can see that the ...
user avatar
  • 1
-2 votes
3 answers
70 views

How to remove forward slashes from file in unix shell using sed? [duplicate]

I have the following file : / /FUSE // Bank S.A / N.V /F I want to remove all forward slashes (/), so that the result looks like this: FUSE Bank S.A N.V F How to do so using sed? Ive tried using ...
user avatar
0 votes
1 answer
60 views

How can I get the size of a variable, string, or array in bytes?

For example, if I build an array: $ array1=($(find /etc -mindepth 1 -maxdepth 1 -type d)) $ echo ${#array[@]} 105 $ for each in ${array1[@]}; do echo $each ; done /etc/alternatives /etc/apache2 /etc/...
user avatar
0 votes
1 answer
101 views

Makefile subshell if condition

So I have the following code: .PHONY: test test: ifeq ($(grep -Fxq "substring" ./file && echo 1 || echo 0), 1) //do something else //do other thing endif The idea is simply ...
user avatar
  • 25
0 votes
1 answer
41 views

Count the number of words ends with specific character using Bash

Heys guys I wanted to count the number of words that end with 's' from this URL, https://matt.might.net/articles/what-cs-majors-should-know/ Here's what I did curl https://matt.might.net/articles/what-...
user avatar
1 vote
1 answer
52 views

Bash for every x files create sub folder and move files there

I am looking for an alternative dash command I can use as I am trying to execute the code from within python and due to the fact that the sub folder has 100's of files I am am trying to group into ...
user avatar
1 vote
3 answers
66 views

concatenate all the text files in all the sub folders into one giant text file

Ok so the situation is that I have an unknown number of sub-directories that all follow the same naming profile folder0, folder1, folder2, folder3 etc Now each folder will have 3 text files and these ...
user avatar
1 vote
0 answers
98 views

How can I execute a command in a shell script in a loop and in background mode?

In Restart mplayer stream process on loss of internet connection -> use while true an answer is given to restart an external command of a shell script - mplayer - after a connection loss. One ...
user avatar
0 votes
2 answers
61 views

How does ls [[:punct:]]* work?

I am learning bash. Currently, I have a file called _something.txt and when I use: ls [[:punct:]]* I get the following error: ls: invalid option -- 'V' What am I doing wrong?
user avatar
  • 1
0 votes
0 answers
28 views

Oneliner to archive, test the archive and remove source folder or stop if error

I've made my small shell function to un-compress the latest archive in a specific folder, then use it (code removed for sake of clarity), and then make an archive, test it and if everything is ok, ...
user avatar
0 votes
1 answer
33 views

stdin/stderr redirection, piping and color

I've seen a zillion permutations on this question but I think none is an exact duplicate. Can I copy only stderr (or eventually stderr and stdin) to a file, while keeping the colorized output on the ...
user avatar
2 votes
1 answer
141 views

replace character only when it's not inside curly braces (sed, awk, tr, ...)

I have a string called "desktops" that look like this: desktops="1 2 %{F#990000}3%{F-} 4 5 6 7 8 9 0" The 6 digits after the "#" represent an RGB color, so the 3rd ...
user avatar
0 votes
0 answers
26 views

How can I modify this script so that I can add multiple users by providing arguments?

How can I modify this script so that I can add multiple users by providing arguments (usernames) in the command line instead? I know at the very least I need to remove the read options. I don't want ...
user avatar
  • 1
0 votes
0 answers
15 views

Deluge bash script idea, do not start download with x or less peers at start of download, can someone point me in right research direction?

Not asking someone to do my work for me I know the golden rule of SO/SE but this shell script would be really helpful to me, basically if a download starts with x or less peers, it stops and deletes ...
user avatar
  • 11
0 votes
3 answers
57 views

de-duplicate list but group parts of it

I am compiling some access rules from failed logins and after some piping I arrived at this: cat <<INPUT | sort -k 3,3 --unique Deny from 13.42.98.142 # demo Deny from 13.42.98.142 # test Deny ...
user avatar
0 votes
0 answers
46 views

exec terminate command by the space in single quotes

I have a simple bash script goes like #!/bin/bash set -e SPARK_CONF="--master ... \ ... --conf spark.driver.extraJavaOptions='-verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps' \ --conf ....&...
user avatar
0 votes
2 answers
27 views

Is it possible to generate multiple complete commands with command substitution?

There are many commands that accept multiple parameters, like touch, ls and rm. So that I can use the construct {1..3} to generate multiple parameters for the command. For example: [root@192 ~]# touch ...
user avatar
0 votes
3 answers
106 views

Can I run a script on reboot once?

CentOS9 Keeping in mind this question: https://serverfault.com/questions/1099284/centos-how-to-keep-previous-service-state-on-reboot It seems that it is not possible to achieve what I need. Is that ...
user avatar
1 vote
2 answers
29 views

How to add n number of spaces at frontend

I have a text file : ABC.txt which contain below data I want to to n number of spaces at the frontend of my text file on every line A Apple a day keeps a doctor away B I like to play with Ball C I ...
user avatar
-1 votes
1 answer
55 views

How to remove only 1 line with a certain string

Hi i have a question i have searched everywhere but i cant find it. I want to use the sed command to remove a line containing a certain string. In the text file there are more lines with the same ...
user avatar
1 vote
0 answers
36 views

How to re-enable the `enable` command?

As part of a script to set up a restricted user, I ran the enable -n enable command. For testing purposes I would like to undo that command but I can't find any documentation on how to do so. I know ...
user avatar
  • 131
0 votes
1 answer
114 views

Run bash script in cronjob

i'm trying to automate a script using crontab. I've been searching for some information to help me, but haven't been successful so far. I created the script with the shebang and full paths. #!/bin/...
user avatar
  • 3
0 votes
1 answer
66 views

comparing 2 files unix

I have 2 scripts file.txt and file2.txt file1.txt |col1|col2| |name|mandatory| |age|mandatory| |address|mandatory| |email|mandatory| |country|not-mandatory| file2.txt |col1|col2| |name|gabrielle|...
user avatar
  • 3
0 votes
1 answer
33 views

While loop not outputting to a log file

I am making in-roads to getting a curl script to work as intended, working around some issue that requires pagination. My point is... the curl part works, BUT I can't get the output to log to a file. #...
user avatar
0 votes
1 answer
26 views

Remove particular characters in bash

I’ve got a script that pulls an image from a repo, manipulates it, and stores some info. I’m trying to figure out how to strip the tags that append these variables. A variable might be Nginx:23457@...
user avatar
15 votes
2 answers
2k views

Are there security consequences from not giving printf a format to use?

A well-formed printf usually has a format to use: $ var="Hello" $ printf '%s\n' "$var" Hello However, what could be the security implications of not providing a format? $ printf &...
user avatar
0 votes
0 answers
45 views

writing a private key from a variable to file

I have a scenario where I am exporting private key and writing it to a file. export CLI_KEY_FILE="-----BEGIN PRIVATE KEY----- 3MFPAZh1qa8XsLdlBBwRypZ8QxTzjdYe4trERoWoLMuaZWsAVRzTypCZG0/Hhfin ...
user avatar
  • 85
0 votes
1 answer
51 views

How to increment a persistant, inter-process counter in bash/shell

Here is a minimal example of what I am trying to do: # If it does not exist, create a file to persist the unique identifier if [ ! -f ~/.uid ]; then echo 0 > ~/.uid fi # Increment the unique ...
user avatar
0 votes
1 answer
31 views

FreeBSD - get timezone in Continent/Region format

I have a Linux (bash) script to generate a login message (motd). One of the things it displays is the timezone, using the command timedatectl | grep Time | awk {'print $3'}. The command returns ...
user avatar
  • 77
0 votes
1 answer
32 views

Running a script on every tty

I have a shell script that i want to run on every tty echo "Hello!" i tried adding it to my .bashrc but it only works after you login and it executes even if the terminal isnt a tty
user avatar
0 votes
3 answers
56 views

How to copy all files with same extension between identical directory structures?

I have 2 independent folders A and B. B has many files with the extension .build. Across A there are a fe subdirectories that have the same structure as subdirectories of B. For example A has ...
user avatar
  • 113
-1 votes
2 answers
42 views

Is it possible to output LaTeX in a shell prompt?

There are several "lambda" prompts (i.e., shell prompts which use the $\lambda$ symbol). While these look nice in theory, many fonts do not use the lambda symbol that I like: $\lambdaup$. I'...
user avatar
1 vote
1 answer
40 views

Inserting a file without altering the folder's modification timestamp?

I have hundreds of folders with modification timestamps I'd like to preserve. Now I need to copy a single file into them. Aside from this way... timestamp=$(stat -c %y /foldername) cp /tmp/file.jpg /...
user avatar
  • 315
0 votes
0 answers
46 views

How do I check if there's an existing file ending with .txt using an if statement in unix [duplicate]

I'm trying to write a shell script to check if there's any number of files existing that ends with .txt using an if statement. My problem is I keep getting back the error "binary operator ...
user avatar
2 votes
3 answers
119 views

Performing calculation on the contents of two files

file1 contains 100 as its content, and file2 contains 200. How would I perform calculation on those two numbers in the terminal? Say I would like to perform division on those two numbers. My approach ...
user avatar
0 votes
3 answers
46 views

If the value in the first column is the same, compare second column and if the second one is different save the value in the first column in txt file

I try to write script to get the the values from the first column, which are the same, but have a different value in the second column. Does someone know how to do it ? Example: I have this .txt file: ...
user avatar
1 vote
2 answers
55 views

How to execute a string of bash command with command substitution

Suppose I have a bash variable like this: tmp1='$(echo foo)' or tmp2='`echo foo`' How to achieve foo as result? I know that removing the command substitution from the string should work, but is ...
user avatar
1 vote
1 answer
51 views

Is there a command to get the current memory address of the current shell script/command?

I wanted to do some protection against the memory dumping when users can recover my shell script's commands using the (delete) tag and then recover it from its memory address. So I thought it could ...
user avatar
  • 111
0 votes
1 answer
198 views

Merge all csv files in directory column wise

Say the directory contained 3 csv files: The first csv: Name, John Age, 18 The second csv: Name, Jim Age, 21 The third csv: Name, Amy Age, 22 I would want the result to be: Name, John, Jim, Amy Age,...
user avatar
  • 25
1 vote
1 answer
26 views

Pipefail prevents command substituation from evaluating pgrep?

My goal is to run a pgrep lftp -c command inside a shell script to check if lftp is already running. But the following script never goes beyond "test" output. Only if I remove the pipefail ...
user avatar
0 votes
0 answers
37 views

"make" command not found due to PATH environment error during building FFmpeg with Zsh

I tried to build FFmpeg via terminal (zsh) and encountered this error : Command 'make' is available in the following places * /bin/make * /usr/bin/make The command could not be located because '/usr/...
user avatar
1 vote
1 answer
77 views

How to pass date variable in SFTP data path?

I need to create a path with a variable value which contains yesterday date. I need to get files using SFTP. Here is my try. #/bin/bash TS=$(date -d '30 mins ago' +"%Y%m%d") sshpass -p '12#...
user avatar
  • 51