algo-en
  • Introduction
  • I. Dynamic Programming
    • Dynamic Programming in Details
    • Classic DP: Edit Distance
    • Classic DP: Super Egg
    • Classic DP: Super Egg(Advanced Solution)
    • Class DP: Longest Common Subsequence
    • Classis DP: Game Problems
    • Regular Expression
    • The Strategies of Subsequence Problem
    • Greedy: Interval Scheduling
    • 4 Keys Keyboard
    • What is DP Optimal Substructure
    • Longest Increasing Subsequence
    • KMP Algorithm In Detail
    • House Robber Problems
    • Stock Buy and Sell Problems
  • II. Data Structure
    • Binary Heap and Priority Queue
    • LRU Cache Strategy in Detail
    • Collections of Binary Search Operations
    • Special Data Structure: Monotonic Stack
    • Special Data Structure: Monotonic Stack
    • Design Twitter
    • Reverse Part of Linked List via Recursion
    • What's the Best Algo Book
    • Queue Implement Stack/Stack implement Queue
    • Framework for learning data structures and algorithms
  • III. Algorithmic thinking
    • My Way to Learn Algorithm
    • The Framework for Backtracking Algorithm
    • Binary Search in Detail
    • The Tech of Double Pointer
    • The Key Concept of TwoSum Problems
    • Divide Complicated Problem: Implement a Calculator
    • Prefix Sum Skill
    • FloodFill Algorithm in Detail
    • Interval Scheduling: Interval Merging
    • Interval Scheduling: Intersections of Intervals
    • String Multiplication
    • Pancake Soring Algorithm
    • Sliding Window Algorithm
    • Some Useful Bit Manipulations
    • Russian Doll Envelopes Problem
    • Recursion In Detail
    • Backtracking Solve Subset/Permutation/Combination
    • Several counter-intuitive Probability Problems
    • Shuffle Algorithm
  • IV. High Frequency Interview Problem
    • How to Implement LRU Cache
    • How to Find Prime Number Efficiently
    • How to Calculate Minimium Edit Distance
    • How to Solve Drop Water Problem
    • How to Remove Duplicate From Sorted Sequence
    • How to Find Longest Palindromic Substring
    • How to Reverse Linked List in K Group
    • How to Check the Validation of Parenthesis
    • How to Find Missing Element
    • How to Pick Elements From a Arbitrary Sequence
    • How to use Binary Search
    • How to Scheduling Seats
    • Union-Find Algorithm in Detail
    • Union-Find Application
    • Find Subsequence With Binary Search
    • Problems can be solved by one line
    • How to Find Duplicate and Missing Element
    • How to Check Palindromic LinkedList
  • V. Common Knowledge
    • Difference Between Process and Thread in Linux
    • You Must Know About Linux Shell
    • You Must Know About Cookie and Session
    • Cryptology Algorithm
    • Some Good Online Pratice Platforms
Powered by GitBook
On this page
  • 1. The difference between standard input and variables
  • 2. Why processes running on the background exit upon terminal termination
  • 3. Single-quotes vs. double-quotes
  • 4. How does sudo make commands not found

Was this helpful?

  1. V. Common Knowledge

You Must Know About Linux Shell

PreviousDifference Between Process and Thread in LinuxNextYou Must Know About Cookie and Session

Last updated 4 years ago

Was this helpful?

Translator:

Author:

Although Windows has advantages over Linux in terms of graphical interfaces, due to Windows' limited support for terminal scripts, I still prefer Linux. Using terminal may, at first, seem counterintuitive, but with more familiarity, it can become a timer saver over graphical interfaces.

Instead of demonstrating the usage of Linux terminal, the post focuses on the basic yet confusing gotchas:

  1. The difference between standard input and variables.

  2. Why processes running on the background exit upon terminal termination?

  3. Single-quotes vs. double-quotes.

  4. How does sudo make commands not found?

1. The difference between standard input and variables

The difference between standard input and variables boils down to the question of when to use pipe | and redirecting >, < vs. when to use variables $.

For example, if a shell script to automate ethernet connection locates in my home directory:

$ where connect.sh
/home/fdl/bin/connect.sh

To remove the script with minimal effort, I tried:

where connect.sh | rm

However, the command above is incorrect. The proper way is:

rm $(where connect.sh)

The former attempts to pipe the output from where into the standard input of rm, whereas the latter passes it in as an variable.

Typically standard inputs appear in programming languages as scanf and readline; Variables refer to the literal arguments, args, the main program consumes.

As mentioned in「Linux file descriptor」, Pipe and redirecting aim to use data as standard input. By contrast, $(cmd) reads the output from cmd as variables.

Revisiting the previous example, the source code of rm will certainly prefer receiving variable arguments over standard input to remove a file. In comparison, the cat command accepts both standard input and variables.

$ cat filename
...file text...

$ cat < filename
...file text...

$ echo 'hello world' | cat
hello world

If a command can clog the terminal, then it accepts standard input and vice versa. For example, running "cat" without arguments will suspend (intentionally clog) the terminal to wait for user input and print back the same content.

2. Why processes running on the background exit upon terminal termination

For example, we want to spin up a Django web server on a remote server:

$ python manager.py runserver 0.0.0.0
Listening on 0.0.0.0:8080...

With the server up and running, we can test it through the server's IP address. However, at the same time, the terminal will suspend, not responding to any input, until it detects Ctrl-C/Ctrl-/ and kills the Python process.

With a tailing &, the command won't clog the terminal and will continue to respond to incoming commands. However, the website becomes unavailable once you log out of the server.

To keep the web service available after logging out of the server, consider using this command (cmd &):

$ (python manager.py runserver 0.0.0.0 &)
Listening on 0.0.0.0:8080...

$ logout

Under the hood::

Every terminal is a shell process, and it forks itself to provide child processes to execute commands. Usually, the shell process clogs while waiting for the child processes to exit, not accepting new commands. With a tailing &, the shell process allows issuing new commands. However, when the shell process exits upon the termination of the terminal window, all its child processes will exit.

Nevertheless, commands like (cmd &) move the process under systemd, an OS guarded process that prevents the process from exiting when we close the current terminal.

An alternative approach to background execution is:

$ nohup some_cmd &

nohup functions similarly, but with extensive testing, (cmd &) appears to be more stable.

3. Single-quotes vs. double-quotes

Shells with different flavors behave differently, but with one invariant: for $,(,), single-quote won't trigger evaluation, but double-quote will.

The shell behavior is observable through set -x, which triggers playback:

As shown above, echo $(cmd) and echo "$(cmd)" differ slightly. Look closely, double-quote adds single-quote after evaluation whereas single-quote doesn't.

As a result, if the literal value from $ contains space, we should use double-quote to avoid errors.

4. How does sudo make commands not found

Under certain situations, a command that non-privileged users can execute becomes "not found" when privileged users try to run with sudo:

$ connect.sh
network-manager: Permission denied

$ sudo connect.sh
sudo: command not found

The root cause is that the connect.sh script only exists in the user's environment variables.

$ where connect.sh
/home/fdl/bin/connect.sh

When prefixing sudo, we tell the OS that the sudoer is executing the command, so the OS will search the environment variables of the sudoer(defined in /etc/sudoer) where the connect.sh script doesn't exist.

The solution is to locate the script with a path instead of a name:

sudo /home/fdl/bin/connect.sh
Tianhao Zhou
labuladong