bash : Bad Substitution

The default shell (/bin/sh) under Ubuntu points to dash, not bash. me@pc:~$ readlink -f $(which sh) /bin/dash So if you chmod +x your_script_file.sh and then run it with ./your_script_file.sh, or if you run it with bash your_script_file.sh, it should work fine. Running it with sh your_script_file.sh will not work because the hashbang line will be … Read more

JavaScript – Replace all commas in a string [duplicate]

The third parameter of String.prototype.replace() function was never defined as a standard, so most browsers simply do not implement it. The best way is to use regular expression with g (global) flag. var myStr=”this,is,a,test”; var newStr = myStr.replace(/,/g, ‘-‘); console.log( newStr ); // “this-is-a-test” Still have issues? It is important to note, that regular expressions … Read more

Can’t use ‘\1’ backreference to capture-group in a function call in re.sub() repr expression

The reason the re.sub(r'([0-9])’,A[int(r’\g<1>’)],S) does not work is that \g<1> (which is an unambiguous representation of the first backreference otherwise written as \1) backreference only works when used in the string replacement pattern. If you pass it to another method, it will “see” just \g<1> literal string, since the re module won’t have any chance … Read more

Python Regex escape operator \ in substitutions & raw strings

First and foremost, replacement patterns ≠ regular expression patterns We use a regex pattern to search for matches, we use replacement patterns to replace matches found with regex. NOTE: The only special character in a substitution pattern is a backslash, \. Only the backslash must be doubled. Replacement pattern syntax in Python The re.sub docs … Read more

Command substitution: backticks or dollar sign / paren enclosed? [duplicate]

There are several questions/issues here, so I’ll repeat each section of the poster’s text, block-quoted, and followed by my response. What’s the preferred syntax, and why? Or are they pretty much interchangeable? I would say that the $(some_command) form is preferred over the `some_command` form. The second form, using a pair of backquotes (the “`” … Read more