gcc -v --help 2> /dev/null | sed -n '/^ *-std=\([^<][^ ]\+\).*/ {s//\1/p}'
Note you can tag on a | grep c++ or | grep c to get the standards supported by a particular langauge
Explanation
Step 1: gcc -v --help 2> /dev/null
gcc -v --helpprints a lot of information about GCC, including available command-line options.2> /dev/nullredirects stderr to/dev/null. GCC prints help options to stderr, so this suppresses messages you don’t care about.
Step 2: | sed -n '...'
- The pipe (
|) sends GCC’s output tosed. sed -ndisables the default printing behavior. Normally,sedprints every line after processing;-ntells it to print only when explicitly usingp.
Step 3: The sed script
/^ *-std=\([^<][^ ]\+\).*/ { s//\1/p }
a) Pattern match: ^ *-std=\([^<][^ ]\+\).*
^ *→ matches the start of the line followed by zero or more spaces.-std=→ literally matches-std=.\([^<][^ ]\+\)→ a capturing group:[^<]→ matches any character except<.[^ ]\+→ matches one or more characters that are not a space.
.*→ matches the rest of the line (any characters, zero or more).
This pattern looks for lines like:
-std=c++17
-std=c++20
It avoids matches like -std=<something>.
b) Action block: { s//\1/p }
s//\1/→ substitute the matched pattern with the first capture group\1.p→ print the result.
Effectively, it takes a line like:
-std=c++17
and outputs:
c++17
Summary
- The command extracts all
-std=compiler versions that GCC supports. - It ignores entries starting with
<. - Outputs just the version strings like:
c++98
c++03
c++11
c++14
c++17
c++20
c++23