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.

1
vote
2answers
17 views

Pass variable within json payload in shell script

In the following scenario, which is the way to pass ${MY_ENV_VAR} in the payload? I will have to escape: a) the single quotes of the payload b) the double quotes of the value of the text json ...
6
votes
2answers
2k views

Set difference from two files in unix

I am looking for an efficient way to achieve set difference from 2 files. I do know that grep -vxF -f setA setB gives me B - A. This is what I want, but my files are as follows: setA: 2 4 5 6 set B: ...
1
vote
1answer
70 views

Prevent dangerous commands from going into bash history [duplicate]

What's the easiest way to prevent dangerous commands matching a pattern (like rm) from going into the Bash command history? I'd like to keep them away from the history of the current session, not just ...
2
votes
3answers
967 views

How to recursively source shell scripts?

This question is, if it matters, preferably in the context of Bash, but I would appreciate a cross-language solution. The problem I'm encountering is in the context of "sourcing" a script with ...
233
votes
3answers
94k views

What are the shell's control and redirection operators?

I often see tutorials online that connect various commands with different symbols. For example: command1 | command2 command1 & command2 command1 || command2 command1 && command2 ...
9
votes
2answers
421 views

Why does a brace command group need spaces after the opening brace in POSIX Shell Grammar?

TL;DR: Why does POSIX brace group need spaces after { reserved word but subshell doesn't after reserved word (? POSIX shell grammar defines brace group and subshell as follows brace_group : ...
0
votes
0answers
45 views

How to redirect STDOUT from all executables that output more than one screen of text to a pager?

How to stop all executables to dump long output to STDOUT? In other words, I'd like to see the output which goes directly to a terminal to be truncated to the one screen of text or just altogether ...
1
vote
1answer
44 views

how to start a shell that will display login / password prompt?

lets say that I have a program that is running in tty1 displaying a menu, one of the entry of this menu is to have access to the shell, which command can I start to be able to have a prompt asking for ...
6
votes
3answers
6k views

Getting the current path in .desktop EXEC command

Im trying to make a .desktop file run a .sh which is stored in the same directory as the .desktop. The whole directory needs to be portable and moved from machine to machine. my run script is run.sh ...
0
votes
1answer
31 views

Bash script doesn't executed properly inside docker container

I am trying to run bash script: export LD_LIBRARY_PATH=/usr/lib/fsl/5.0:$LD_LIBRARY_PATH export SUBJECTS_DIR=/output timefmt="%C --- CPU:\t%E real,\t%U user,\t%S sys\t%P\tMem:\t%KkiBavg.,\t%MkiB max.\...
82
votes
8answers
93k views

How can I install the `ll` command on Mac OS X?

I'm using Mac OS X. When I SSH into servers I find the ll command useful, but it's not available on my local machine. How can I install it?
1
vote
1answer
49 views

how to split the matching file pattern files in same directory using if else or for loop condition?

I am having 5 types of files in same dirctory.then i want to split all the files in the same directory and i need to give the splitted file name as dynamic(original file name + additional extexnsion). ...
3
votes
4answers
5k views

ping: show only results

Is it possible to only show the amount of milliseconds when pinging instead of the whole result page? I want to check if my servers are online, so I want to return "OK xyz ms" or "FAIL". I am ...
9
votes
4answers
8k views

Keep exit codes when trapping SIGINT and similar?

If I use trap like described e.g. on http://linuxcommand.org/wss0160.php#trap to catch ctrl-c (or similar) and cleanup before exiting then I am changing the exit code returned. Now this probably won'...
0
votes
0answers
23 views

Update an existing SFV file using CFV

I have an existing sfv file generated using cfv -C -tsfv -rr Now how would I update this file when there are new files added to the directory? Re-generating the whole thing will take a long time (an ...
6
votes
7answers
80k views

Extract date from a variable in a different format

Let me explain you the problem $ date +%c -d "$d" Tue 31 Dec 2013 01:13:06 PM CET $ date +'Today is %F' -d "$d" Today is 2013-12-31 This solution corresponds to current date. But I have one ...
3
votes
1answer
345 views

Passing all files of specific filetype in current and subdirectories

I have constructed an elaborate 500+ character command with many arguments and switches to autosign multiple PDF documents. I'm using JSignPDF and I'd like to use it's batch mode including the ...
0
votes
1answer
16 views

Variable rsync filter parameters

I have an issue with a variable rsync parameter. I concat filtered expression on COMPILE_FILTERS variable. Example : COMPILE_FILTERS='--filter="- js/editor/editorjs/*"' rsync -a $COMPILE_FILTERS \ -...
4
votes
5answers
16k views

What does exit 99 means?

I want to know what is exit 99 and why would one use it and what are the significance uses of it. For example, I'm using exit 99.
0
votes
3answers
27 views

How to slice a horizontal tail of a single line

given file1 1:b:c:4 file2 1:3:47:8:aa:2 file3 1:4:5:8:9:11:aaaaa:2:3:4:88:9:10 I want to find the tail of this line after the ':'. To note each file only contains one line always For example ...
4
votes
3answers
2k views

Why does FOO=bar; export the variable into my environment

If I run FOO=bar docker run -it -e FOO=$FOO debian env That environment variable is not set in the command output for the env command. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/...
-1
votes
1answer
22 views

'if' statement causing 'syntax error near unexpected token `done'' in bash script [on hold]

I'm attempting to write a script that creates a new file within a loop every 5 iterations : #!/bin/bash counter=1 timestamp="$(($(date +%s%N)/1000000))" fileName="file"$timestamp".txt" echo $...
0
votes
3answers
60 views

Call a function with an argument inside a function that needs another argument

I don't know how else to word this so I'll do my best to explain. I have a function main() { if [[ "$1" = "-f" ]] then ($sendFirstEmail) "$1" elif [[ "$1" = "...
2
votes
8answers
1k views

Is there a shell command or utility for caching process output?

Hope this is the right place to ask. Is there a unix tool that works similarly to this? # invokes echo, saves to cache (using command with arguments as key), returns "hi" cache 10 echo hi # wait 2 ...
0
votes
0answers
31 views

Separate stdout of an application

I have an application that takes a few command line parameters, then prints some text to the terminal, then starts writing data to a file. It has a parameter I can use to define the file name to write ...
-1
votes
1answer
37 views

how to get expanded path from `ls -l /a/*/b` to a variable

I created a folder structure like the following mkdir -p test/f{1..4}/inside while running ls -l test/*/ i got test/f1/: total 0 drwxr-xr-x 2 s ff 68 Jun 21 18:37 inside test/f2/: total 0 ...
224
votes
14answers
401k views

How can I get the size of a file in a bash script?

How can I get the size of a file in a bash script? How do I assign this to a bash variable so I can use it later?
1
vote
2answers
32 views

how to count characters before/after the first space in line?

I want to count the number of characters after the first space. So for example, the line may be like this: Iam a bunny N = The number of characters after the first space is 7 (a bunny) So I came up ...
0
votes
0answers
56 views

How can I prevent execution of a command in a Linux shell? [duplicate]

I am trying to find a way to do this: any command that does not match the pattern "hh update D*" will be blocked in folder /A/B/C Practical example of what I am trying to achieve: In folder /A/B/C, ...
83
votes
12answers
231k views

How to compare two dates in a shell?

How can two dates be compared in a shell? Here is an example of how I would like to use this, though it does not work as-is: todate=2013-07-18 cond=2013-07-15 if [ $todate -ge $cond ]; then ...
0
votes
1answer
24 views

automatically detect date format from string in linux?

I have a date 20190620_113859 which is from date +%Y%m%d_%H%M%S Is it possible to detect that +%Y%m%d_%H%M%S format from 20190620_113859 as input like $ date --detect-format '20190620_113859' %Y%m%...
2
votes
1answer
1k 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 ...
8
votes
2answers
7k views

How many users does Linux support being logged in at the same time via SSH?

I know Linux supports multiple users being logged in at the same time. But what's the maximum number of users that can be logged into Linux at the same time? I see there are there are 69 tty files (...
24
votes
3answers
4k views

How can I detect if I'm in a subshell?

I'm trying to write a function to replace the functionality of the exit builtin to prevent myself from exiting the terminal. I have attempted to use the SHLVL environment variable but it doesn't seem ...
7
votes
5answers
362 views

How can I display ssh commands executed from another machine?

Can I see what other users type/execute via SSH in my server via bash or shell?
2
votes
2answers
1k views

How to display commands executed via ssh and their user's IP?

I asked a question before, but I didn't get a satisfying answer in : How can I display ssh commands executed from another machine? so I'll ask this time precisely. How to display commands and their ...
9
votes
3answers
1k views

What is the exact difference between a “subshell” and a “child process”?

According to this and this, a subshell is started by using parenthesis (…). ( echo "Hello" ) According to this, this and this, a process is forked when the command is started with a & echo "...
1
vote
1answer
3k views

Difference between '>' and '-gt'

What's the difference between these: SEC=$(mysql -hxxx -Pxxx -uxxx -pxxx -se "SELECT Seconds FROM tablename") SEC=100 Both return the same result (100). But when I test it with: if [ ${SEC} > 1 ...
103
votes
7answers
368k views

Using the not equal operator for string comparison

I tried to check if the PHONE_TYPE variable contains one of three valid values. if [ "$PHONE_TYPE" != "NORTEL" ] || [ "$PHONE_TYPE" != "NEC" ] || [ "$PHONE_TYPE" != "CISCO" ] then echo "Phone ...
10
votes
3answers
8k views

Best practice to use $? in bash?

When I read this answer about $? another question comes to mind. Is there any best practice for how to use $? in bash? Let's have a example: We have a linear script and I we would like to know that ...
0
votes
1answer
1k views

linux/shell command to control screen brightness in android

I am working on a kiosk project using an android tablet.If there is no input power to the kiosk for a long time then, the tablet will eventually shutdown.In order to auto-reboot the tab when the power ...
1
vote
2answers
44 views

Why is SHELL pointed to /bin/sh in a Csh script?

Yes, I know C-shell is bad. No, I did not choose C-shell. Yes, I would prefer to use a proper shell. No, I cannot switch to a better shell. I have a very simplistic script: /tmp/env_test.csh #!/...
0
votes
1answer
41 views

Create a separate file containing rows sharing one specific column with same value!

For instance, I have a data like below: Transmit.123 Invite Members A Transmit.123 Invite Members GC Transmit.111 Invite Members F Transmit.222 Invite Members DE Transmit.222 Invite Members H ...
-4
votes
0answers
25 views

write the command to find the wild character in unix [closed]

I have one file with 10 columns . Need to check which column in having wild character in file through unix command like qw|23|er5|ght|%ft.f%\|Tar need to know which column in having extra ...
0
votes
1answer
37 views

How to know from which bash version a built-in is made available?

Is it possible to know from which specific version of bash a particular built-in is made available from the command-line? Is it only possible by browsing through release notes for each of the versions ...
0
votes
1answer
44 views

Make alias for shell scripts

I'm working my way through the Wicked Cool Shell Scripts book. I've created a script, following the book's code, which can be found here. I'm trying to make an alias for this script so I can just call ...
6
votes
3answers
196 views

Redirecting output only on a successful command call

I want to redirect the output of a command (diff in this case) to a file but only if there is a difference in files I'm comparing. For example, imagine I have three files a, b, and c where a and b are ...
73
votes
13answers
74k views

How can I set my default shell to start up tmux

I would like my default bash shell to go straight into tmux instead of my always having to type tmux every time.
0
votes
3answers
66 views

I have if else question but I am not sure the answer is true? [duplicate]

I am running this on Linux: $ number=4 $ test $number -eq 4 $ echo $? 0 $ test $number -lt 2 $ echo $? 1 Are the results (0 and 1) true ?
126
votes
11answers
127k views

How do you move all files (including hidden) from one directory to another?

How do I move all files in a directory (including the hidden ones) to another directory? For example, if I have a folder "Foo" with the files ".hidden" and "notHidden" inside, how do I move both ...