bash4

Mac: Virtual Shell Bash Version does not Match Installed Version [duplicate]

你离开我真会死。 提交于 2021-01-20 08:59:35
问题 This question already has answers here : Associative arrays: error “declare: -A: invalid option” (10 answers) Closed 2 years ago . I am trying to create shell scripts that make use of Bash 4.0's features. I am on a Mac using zshell as my main shell, and I have bash 4.0 installed via Homebrew. When I run bash --version from iTerm, I get: GNU bash, version 4.4.23(1)-release (x86_64-apple-darwin17.5.0) Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later

Mac: Virtual Shell Bash Version does not Match Installed Version [duplicate]

这一生的挚爱 提交于 2021-01-20 08:59:12
问题 This question already has answers here : Associative arrays: error “declare: -A: invalid option” (10 answers) Closed 2 years ago . I am trying to create shell scripts that make use of Bash 4.0's features. I am on a Mac using zshell as my main shell, and I have bash 4.0 installed via Homebrew. When I run bash --version from iTerm, I get: GNU bash, version 4.4.23(1)-release (x86_64-apple-darwin17.5.0) Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later

How to change current working directory inside command_not_found_handle

十年热恋 提交于 2020-01-02 05:20:47
问题 I'm trying to write a not found handle in Bash that does the following: If $1 exists and it's a directory, cd into it. If $1 exists inside a user defined directory $DEV_DIR , `cd into it. If the previous conditions don't apply, fail. Right now I have something like this: export DEV_DIR=/Users/federico/programacion/ function command_not_found_handle () { if [ -d $1 ]; then # the dir exists in '.' cd $1 else to=$DEV_DIR$1 if [ -d $to ]; then cd $to echo `pwd` else echo "${1}: command not found"

place a multi-line output inside a variable

别来无恙 提交于 2019-12-23 08:03:03
问题 I'm writing a script in bash and I want it to execute a command and to handle each line separately. for example: LINES=$(df) echo $LINES it will return all the output converting new lines with spaces. example: if the output was supposed to be: 1 2 3 then I would get 1 2 3 how can I place the output of a command into a variable allowing new lines to still be new lines so when I print the variable i will get proper output? 回答1: Generally in bash $v is asking for trouble in most cases. Almost

place a multi-line output inside a variable

天大地大妈咪最大 提交于 2019-12-23 08:01:30
问题 I'm writing a script in bash and I want it to execute a command and to handle each line separately. for example: LINES=$(df) echo $LINES it will return all the output converting new lines with spaces. example: if the output was supposed to be: 1 2 3 then I would get 1 2 3 how can I place the output of a command into a variable allowing new lines to still be new lines so when I print the variable i will get proper output? 回答1: Generally in bash $v is asking for trouble in most cases. Almost

place a multi-line output inside a variable

一曲冷凌霜 提交于 2019-12-23 08:01:17
问题 I'm writing a script in bash and I want it to execute a command and to handle each line separately. for example: LINES=$(df) echo $LINES it will return all the output converting new lines with spaces. example: if the output was supposed to be: 1 2 3 then I would get 1 2 3 how can I place the output of a command into a variable allowing new lines to still be new lines so when I print the variable i will get proper output? 回答1: Generally in bash $v is asking for trouble in most cases. Almost

How to iterate over associative arrays in Bash

吃可爱长大的小学妹 提交于 2019-12-17 03:45:08
问题 Based on an associative array in a Bash script, I need to iterate over it to get the key and value. #!/bin/bash declare -A array array[foo]=bar array[bar]=foo I actually don't understand how to get the key while using a for-in loop. 回答1: The keys are accessed using an exclamation point: ${!array[@]} , the values are accessed using ${array[@]} . You can iterate over the key/value pairs like this: for i in "${!array[@]}" do echo "key : $i" echo "value: ${array[$i]}" done Note the use of quotes

In BASH, Is there any way to find exactly the IP address for all interfaces? And remove all other information's?

北城余情 提交于 2019-12-13 17:34:30
问题 How can i get only IP and which interface IP it is? So that i can keep a record file such as realtime.ini 1 - test.sh #!/bin/bash ipstring ='inet (.*)' for i in $(ip addr); do echo $i #on found it write down to my realtime.ini as a list for future query done 2 - realtime.ini em1,192.168.1.2 lo,127.0.0.1 wlan0,<not found> Follow up: Just for single ip: $ ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}' 192.168.1.2 回答1: This is not terribly elegant, nor is bash, but you can

bash script - iterative merge files on a directory

你离开我真会死。 提交于 2019-12-13 04:48:22
问题 Hello I want to iteratively merge the files in input_directory and put the merged ones in output_directory. Suppose in input_directory I have : file1.txt, file2.txt, file3.txt I want to the output directory to contain the following files: merge1.txt : same as file1.txt merge2.txt : merge file1.txt file2.txt merge3.txt : merge file1.txt file2.txt file3.txt I have almost no experience with bash scripts, it is obvious that it can be done with an iterator in a for loop, but I don't know how.

How to parse CSV file for long row in Bash?

拥有回忆 提交于 2019-12-10 12:19:50
问题 I have IDs in the first column of data.csv with headers. I want to skip the header and store column 1 values in the variable ids as 102 103 104 ... . Pseudocode in the line ids.append($col1) where I want to append the current row value to the end of the line with a space # http://stackoverflow.com/a/4286841/54964 while IFS=, read col1 do ids.append($col1) # Pseudocode done < data.csv data.csv 102 103 104 Expected output ids=( 102 103 104 ) OS: Debian 8.5 Bash: 4.3.30(1) 回答1: With GNU bash and