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
212 votes
13 answers
387k views

Tool to monitor folder for new files and run command whenever new file is detected

How can I immediately detect when new files were added to a folder within a bash script? I would like the script to process files as soon as they are created in the folder. Are there any methods aside ...
4 votes
2 answers
6k views

sh: which: line 1: syntax error: unexpected end of file; sh: error importing function definition for `which'

We are running: Red Hat Enterprise Linux release 8.5 (Ootpa). We allow the server to update weekly using "yum -y update". We recently began receiving the following errors when executing ...
5 votes
1 answer
1k views

convert windows path to unix path inside wine application command line

I'm running a windows application in wine, which executes configurable commands to run external applications (e.g. an diff editor to show differences between two source files). So these commands are ...
-1 votes
2 answers
46 views

Output date into specific column using bash

I want to output the date into a csv column using a bash script ignoring the header. I have this but it only gives the day of the week abbreviated and not does not place it in a specific column: date |...
0 votes
1 answer
2k views

How to check if a database exists in teradata while firing a sqoop import command in shell script?

I'm trying to fire a sqoop import command from the shell script. It is working fine as expected. But, if suppose the database is missing in the Teradata then it needs to throw the error and should not ...
3 votes
5 answers
9k views

Is it a good practice to delete all variables at the end of a script?

Is it good to delete the variables used in a shell-script at the end of a script. rm -rf $abc rm -rf $def or unset $abc unset $def and so on. Is this actually a good practice?
122 votes
6 answers
95k views

How to run a command that involves redirecting or piping with sudo?

I am trying to follow what I assume is best practices of using sudo instead of root account. I am running a simple concat file operation such as: sudo echo 'clock_hctosys="YES"' >> /etc/conf....
0 votes
3 answers
89 views

Why does < exist to redirect to stdin?

Question Why do shells implement alternative means like <<<, < <(command) and < /dev/fd/* to redirect something to stdin when pipes do exist? Example The | way (classic pipes) echo '...
0 votes
0 answers
27 views

How to get WID and PID

I'm wondering how to get WID and PID of windows. More specifically, when I open a terminal and another window through that, for example xclock, I want to get WID of that terminal and PID of window ...
3 votes
1 answer
1k views

What are the purposes of `sudo -v &&` and `sh /dev/stdin` here?

From https://calibre-ebook.com/download_linux sudo -v && wget -nv -O- https://download.calibre-ebook.com/linux-installer.sh | sudo sh /dev/stdin What is the purpose of sudo -v && ...
0 votes
1 answer
37 views

Does running exec command preserves environment variables of the calling shell? If so, why?

From my understanding exec command replaces the calling shell program with the command without forking a child process. Also if I understand correctly exported variables are the ones that are passed ...
267 votes
19 answers
430k views

How to add a newline to the end of a file?

Using version control systems I get annoyed at the noise when the diff says No newline at end of file. So I was wondering: How to add a newline at the end of a file to get rid of those messages?
5 votes
2 answers
24k views

Deleting older log files

There are some log files created after a certain amount of time with the time stamp. /mylog/path Log_file_2018-07-19-22-55-31Z.tgz Log_file_2018-07-20-01-29-11Z.tgz Log_file_2018-07-20-10-36-49Z.tgz ...
299 votes
15 answers
458k views

How to monitor CPU/memory usage of a single process?

I would like to monitor one process's memory / cpu usage in real time. Similar to top but targeted at only one process, preferably with a history graph of some sort.
1 vote
2 answers
1k views

force output to input (buffer) like "\033[6n" in a script

I have tried using pipes and redirections to have (C program or scripts) output end up on the input buffer, the way printf "\033[6n" does, but no positive results. Does anyone know how this ...
6 votes
2 answers
822 views

How do I keep a dash shell script running despite any error?

I have a dash shell script that is intended to run to the end of file: #!/usr/bin/dash # File b_shell . a_nonexistent_file echo "I want to print this line despite any previous error." ...
6 votes
2 answers
3k views

How can I customize $PROMPT_COMMAND without overwriting the default (if present)?

Mac's Terminal comes with a default PROMPT_COMMAND that checks the history and updates the current working directory (title of the tab): Add echo $PROMPT_COMMAND to the top of your .bash_profile and ...
0 votes
4 answers
191 views

using aliases in function?

I have a script yummy.sh #!/bin/bash alias yumy='yum install -y ' yumprovision() { yumy } When I run this script got this bash: yumy: command not found why it's not loading alias in function?...
13 votes
5 answers
44k views

How to ssh forwarding with AllowTcpForwarding set to no?

There is some development I need to do on some remote box. Fortunately, I have shell access, but I need to go through a gateway that has AllowTcpForwarding set to false. I took a peak at the docs and ...
8 votes
3 answers
925 views

Combining brace expansion and output redirection

Often, I use parameter expansion on the command-line, such as cp a.{ext1,ext2} which is expands to cp a.ext1 a.ext2 However, in many cases I do not use cp, but another tool, let's call it cat, and ...
1 vote
2 answers
41 views

zsh completion: complete block devices in /dev

for my script, I need to complete block devices in /dev, so that when I do my_script.sh <TAB> it will complete /dev/ and offer me all disks that are listed in /dev (not in subdirectories). So ...
-1 votes
1 answer
2k views

Execute an shell script with sudo inside from php

I want to execute a command line command as root from a php file. In my case I want to view the crontab file in my php page. Running the sudo command in php is not recommended and on my raspberry pi ...
0 votes
0 answers
42 views

Where should I put my personal shell script in terms of safety?

I am learning sh to make some customized function. I am looking for a safety location for the personal script, since I check from stack exchange, some say that put personal script under ~/bin may ...
0 votes
1 answer
39 views

ssh EOF in bash script - how to store value of command?

I need to basically copy only the last created folder in a directory of many folders within an SSH session. I have the correct find command to do this, but I just can't seem to store the result within ...
0 votes
2 answers
2k views

Exclude the output from ssh and only log the error if found

typeset -f | sshpass -e ssh -o StrictHostKeyChecking=no user@${IPADDRESS} " $(cat); IFERROR=$(checkscript); echo "$IFERROR"" > ${SSHTEMPFILE} 2>&1 This line...I can't exclude the "...
3 votes
1 answer
3k views

nmap won't recognize a hostname on my local area network

Scanning the machine on the LAN with my laptop, I got this: gabriele @ osiris { ~ } [ Tue Oct 07 ] [ 08:49 PM] nmap -sP 192.168.1.236 Starting Nmap 6.40 ( http://nmap.org ) at 2014-10-07 20:50 CEST ...
45 votes
7 answers
125k views

How to use inotifywait to watch a directory for creation of files of a specific extension

I have seen this answer: You should consider using inotifywait, as an example: inotifywait -m /path -e create -e moved_to | while read path action file; do echo "The file '$file' ...
0 votes
1 answer
24 views

nohup process pid not visible in new shell but still in old shell

I open and connect an ssh terminal window shell to my server and run this: nohup sleep 50000 & When I then run ps I see this: PID TTY TIME CMD 1 ? 00:00:00 bash 56 ? ...
4 votes
1 answer
132 views

git: inconsistent behavior when using filter to strip comments

I am using filter to remove comments before committing config files in git: $ cat .git/config [filter "stripcomments"] clean = "stripcomments" $ cat .git/info/attributes /etc/* ...
21 votes
7 answers
24k views

Fastest and most efficient way to get number of records (lines) in a gzip-compressed file

I am trying to do a record count on a 7.6 GB gzip file. I found few approaches using the zcat command. $ zcat T.csv.gz | wc -l 423668947 This works but it takes too much time (more than 10 minutes ...
1 vote
0 answers
36 views

How does the system decide which shell runs a script without a shebang line? [duplicate]

I have this "script" on my Arch Linux system: $ cat ~/scripts/foo.sh ps -hp $$ It will simply run ps on its own PID. But the script has no shebang line. I was expecting that in the absence ...
265 votes
5 answers
66k views

Why is using a shell loop to process text considered bad practice?

Is using a while loop to process text generally considered bad practice in POSIX shells? As Stéphane Chazelas pointed out, some of the reasons for not using shell loop are conceptual, reliability, ...
0 votes
2 answers
36 views

How to prepend text to each echo in a called script?

Let's say I have two scripts, one calls the other, how can I prepend text to each echo to visually indicate the echo comes from the called command? a.sh #!/usr/bin/env sh echo - BEGIN ./b.sh # Append &...
3 votes
3 answers
5k views

What this error message mean please: Ignoring request to auto-attach. It is disabled for org "15824072" because of the content access mode setting

When trying to subscribe my centos 8 to a redhat repos, I got the error bellow. Can someone explains what do I need to do ? [root@thiecentos8 ~]# subscription-manager attach --auto Ignoring request ...
0 votes
1 answer
1k views

"export VAR=thing && someCommand" reports "export: `&&': not a valid identifier"

I'm trying to set an environment variable and execute a command in a single line, like this: export PUB_CACHE=/app/src/.pub-cache && dart-sdk/bin/dart server.dart (Don't ask why... it's ...
38 votes
9 answers
68k views

How to remove blank lines from a file (including tab and spaces)?

I want to remove all empty lines from a file. Even if the line contains spaces or tabs it should also be removed.
0 votes
2 answers
85 views

Dangers of executable file with '#' in name?

This might be a FAQ--happy to have the question closed if it's a dupe--but I'm not sure how to find it if so. I'd also be happy with an answer that simply told me how to find the relevant ...
90 votes
7 answers
84k views

"trap ... INT TERM EXIT" really necessary?

Many examples for trap use trap ... INT TERM EXIT for cleanup tasks. But is it really necessary to list all the three sigspecs? The manual says: If a SIGNAL_SPEC is EXIT (0) ARG is executed on ...
-4 votes
1 answer
283 views

$PS1 vs $PROMPT_COMMAND in bash?

I'd like to customize my shell's prompt. What are the differences between $PS1 and $PROMPT_COMMAND, and what variable should I use? Some historic conventions (of past shells/posix) to find out what is ...
11 votes
1 answer
849 views

Make a trailing newline execute a command when pasted into the shell

A couple of months ago, a system update caused my shell to no longer interpret trailing newlines as an enter when pasting a command into the terminal. For example, if I write ls at my prompt and then ...
-1 votes
1 answer
82 views

On what occasions should you use 'sh -c' instead of directly executing a program?

I often see stuff like sh -c "curl -o …" Under what circumstances should you use that instead of just curl -o …
3 votes
1 answer
47 views

tar files with backslahes on Debian

I need to copy files that contain (Windows-style) backslahes from a Debian 10 pod. I'm having trouble with GNU tar 1.30 archiving files with backslashes: docker run --rm -it debian:10 bash inside the ...
10 votes
3 answers
1k views

Is it possible to send data to the the shell of another pseudo-terminal?

This image shows how pseudo-terminals work: If I have two terminal emulators open, I can send the string "Hello" from the first terminal emulator to the second terminal emulator: echo "Hello" > /...
0 votes
3 answers
40 views

Shell extract full word matching partial pattern

I need to extract a word from a line in a shell script, I've seen similar examples though none of which worked. Take the example sentence word1 word2 word3/pattern/word4.word5 word6 Given pattern it ...
275 votes
12 answers
414k views

How do I convert an epoch timestamp to a human readable format on the cli?

How do I convert an epoch timestamp to a human readable format on the cli? I think there's a way to do it with date but the syntax eludes me (other ways welcome).
2 votes
1 answer
2k views

Command not found message [duplicate]

When running invalid commands without any parameters or options, in my experience we get only two kinds of messages which are exemplified below: ~$ Date No command 'Date' found, did you mean: ...
-1 votes
1 answer
82 views

Why use "tail -n 1" for finding the nth line of a file in Unix?

I've noticed that people typically use head -n | tail -n 1 to find the nth line of a file in Shell. For example, if I was asked to find the 7th line of a file, why not just use head -7 instead of ...
0 votes
0 answers
40 views

Desktop entry doesn't run the script

I have the following desktop entry on my ~/.local/share/applications directory: [Desktop Entry] Type=Application Version=1.0 Name=FetchSecurity Exec="/home/user/Pictures/security/...
113 votes
6 answers
124k views

How to download an archive and extract it without saving the archive to disk?

I'd like to download, and extract an archive under a given directory. Here is how I've been doing it so far: wget http://downloads.mysql.com/source/dbt2-0.37.50.3.tar.gz tar zxf dbt2-0.37.50.3.tar.gz ...
0 votes
0 answers
40 views

The debian package with dpkg -i works fine, but the same with "apt install" throws error

Trying to install debian package from server with apt install command. dpkg: error processing package <pkg_name> (--configure): installed <pkg_name> package post-installation script ...

1
2 3 4 5
236